selfの引数はメソッドの引数の個数に数えないのでしょうか?
class Book: def __init__(self, title, author, price): self.title = title self.author = author self.price = price def sale(self, discount_rate): return int(self.price * (100 - discount_rate) / 100) def main(): book_1 = Book('こころ', '夏目漱石, 10000) print('5割引:' + str(book_1.sale(50)) + '円') if __name__ == '__main__': main()
というコードがあります。
print('5割引:' + str(book_1.sale(50)) + '円')
とsale()メソッドの引数は1つですが、
def sale(self, discount_rate):
ではselfと discount_rate の2種類がわたされています。
本来、
print('5割引:' + str(book_1.sale(self,50)) + '円')
と書くべきのように思えますが。。。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。