That is because the value of email was computed and saved when you ran __init__()
. Just because you change self.first
and self.last
doesn't mean self.email
will be affected, because it is not saved in terms of its relationship to those variables. It is saved as a string.
Example:
a = "hello"
b = "world"
c = a + b
a = "goodbye"
print(c)
>>> "hello world"
Here, c
was set to a+b
and that value was saved when that line was executed. Changing a
after we have already given c
its value won't change it. It is already set, and we would have to do c = a+b
again to get it to print out goodbye world
.
In your case, you'd have to modify it in the setter:
@fullname.setter
def fullname(self,name):
first,last = name.split(' ')
self.first = first
self.last = last
self.email = self.first + '.' + self.last + '@email.com'
However that is not good practice, as the setter should only change the specific variable you're accessing. That is why the tutorial you were reading had a function for email: If you want the it to be dynamically computed based on whatever the values of first
and last
are at the moment you check it, you should always use a function:
def email(self):
return self.first + '.' + self.last + '@email.com'
print(emp1.email())
If you want, you can use the property
decorator like the tutorial does:
@property
def email(self):
return self.first + '.' + self.last + '@email.com'
This is just syntactical sugar to allow you to call it as before:
print(emp1.email)
Instead of having to call it like a function, even though that is all it is.
As a sidenote, I'd recommend using string formatting, which is generally preferred to using +
to join strings together in cases like this:
return f"{self.first}.{self.last}@email.com"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…