Jan-07-2024, 11:54 AM
hi
the below code is in :https://docs.python.org/3/howto/descriptor.html
why is called __set__ and __get__ ? how does it know to call them? is age in line age = LoggedAgeAccess() the same as age in __init__ method ? please give some explanation.
thanks
the below code is in :https://docs.python.org/3/howto/descriptor.html
import logging
logging.basicConfig(level=logging.INFO)
class LoggedAgeAccess:
def __get__(self, obj, objtype=None):
value = obj._age
logging.info('Accessing %r giving %r', 'age', value)
return value
def __set__(self, obj, value):
logging.info('Updating %r to %r', 'age', value)
obj._age = value
class Person:
age = LoggedAgeAccess() # Descriptor instance
def __init__(self, name, age):
self.name = name # Regular instance attribute
self.age = age # Calls __set__()
def birthday(self):
self.age += 1 # Calls both __get__() and __set__()in the above code, in the last lines is written that calls _set__() and calls both __get__() and ....why is called __set__ and __get__ ? how does it know to call them? is age in line age = LoggedAgeAccess() the same as age in __init__ method ? please give some explanation.
thanks
