classmethodデコレータを使ったコンストラクタのオーバーライドの例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See https://pandanote.info/?p=9956 for details. | |
class Expense: | |
amount = 0 | |
reason = "" | |
remark = "" | |
def __init__(self, amount, reason, remark): | |
self.amount = amount | |
self.reason = reason | |
self.remark = remark | |
@classmethod | |
def no_remark(cls, amount, remark): | |
return cls(amount, "", remark) | |
@classmethod | |
def copy(cls, expense): | |
return cls(expense.amount, expense.reason, expense.remark) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment