Method attribute is not updating itself even though I'm clearly assigning a different value to it

后端 未结 4 1839
闹比i
闹比i 2021-01-07 09:38

Upon updating a value, the __init__ method still uses the old attribute value.

class Email:
    def __init__(self, name):
        self.name = na         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-07 09:59

    The simplest fix would be to make email a property, rather than an attribute you set in __init__.

    class Email:
    
        def __init__(self, name):
            self.name = name
            # self.email = self.name + "@hotmail.com"
    
        @property
        def email(self):
            return f'{self.name}@hotmail.com'
    
        def details(self):
            return f'{self.name} | {self.email}'
    

提交回复
热议问题