Skip to content Skip to sidebar Skip to footer

How Python Variables Name In Getter And Setter Method Are Different To The Name In Its Constructor?

I am confused to a example of property from python cookbook. class Person: def __init__(self, first_name): self.first_name = first_name @property def firs

Solution 1:

self.first_name/people.first_name is the setter method-turned-property def first_name(self, value), while self._first_name is the actual attribute holding the value. The constructor uses the setter to set the initial name, it doesn't directly assign to the property.

  1. constructor receives value as function parameter
  2. constructor assigns to property first_name which
  3. invokes the setter def first_name which
  4. assigns to the property _first_name

Note that there's another confusing typo in the constructor, it should be self.first_name = first_name.

Post a Comment for "How Python Variables Name In Getter And Setter Method Are Different To The Name In Its Constructor?"