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

后端 未结 4 1837
闹比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 10:06

    What went wrong in the OP?

    If we open up the person object right after you instantiated it with the line person = Email("James"), it would be something like this:

    person {
    "name"  : "James", 
    "email" : "James@hotmail.com"
    }
    

    If we update the name variable in this person object as you did with person.name = "Michael", and open up the person object again, it would be:

    person {
    "name"  : "Michael", 
    "email" : "James@hotmail.com"
    }
    

    Please note that at this stage, it is still the same person object we instantiated earlier, and the email variable is not changed from its previous state, because we didn't do anything about it, yet.


    The answer posted by @chepner is very nice and clean, by setting email field as a property, and by setting it dynamically using the name variable. Below is a copy of @chepner 's code:

    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}'
    

    What is property and how is it used?

    property is a Python Built-in Function, a property object has these methods:

    • getter: used to get an attribute value
    • setter: used to set an attribute value
    • deleter: used to delete an attribute value

    Now, what exactly is happening in @chepner 's answer? With the lines below, we are setting the email as a property:

        @property
        def email(self):
            return f'{self.name}@hotmail.com'
    

    And this decorated function can also be used as a getter, e.g. person.email.

    Note that in here we didn't link it to a variable (as shown in the Python documentation example), if we want / need, we can do so by replacing the return statement and set an _email variable in __init__:

        def __init__(self, input_name):
            self._name = input_name
            self._email = f'{self._name}@hotmail.com'
    
        @property
        def email(self):
            return self._email
    

    And then, as for the setter and deleter, we can create them as:

        @email.setter
        def email(self, input_email):
            self._email = input_email
    
        @email.deleter
        def email(self):
            del self._email
    

    Note that the getter, setter, and deleter methods all have the same name as the property, just different decorators.

    From the question description it is not needed to support updating the email address separately, but just following this example here, if we run person.email = "Michael@hotmail.com", because email is a property, it is triggering the setter to set the value to the _email variable.


    Going back to the details method in @chepner 's answer:

        def details(self):
            return f'{self.name} | {self.email}'
    

    By doing self.email we are triggering the getter to return the email address, which is dynamically generated in that return statement return f'{self.name}@hotmail.com'.

提交回复
热议问题