As you see, the function full_name
returns a string with the persons first and last name.
What the @property
decorator does, is declare that it can be accessed like it's a regular property.
This means you can call full_name
as if it were a member variable instead of a function, so like this:
name = person.full_name
instead of
name = person.full_name()
You could also define a setter method like this:
@full_name.setter
def full_name(self, value):
names = value.split(' ')
self.first_name = names[0]
self.last_name = names[1]
Using this method, you can set a persons full name like this:
person.full_name = 'John Doe'
instead of
person.set_full_name('John Doe')
P.S. the setter above is just an example, as it only works for names that consist of two words separated by a whitespace. In real life, you'd use a more robust function.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…