Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
question about descriptor
#1
hi
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
Reply


Messages In This Thread
question about descriptor - by akbarza - Jan-07-2024, 11:54 AM
RE: question about descriptor - by Gribouillis - Jan-07-2024, 02:50 PM
RE: question about descriptor - by akbarza - Jan-08-2024, 07:35 AM
RE: question about descriptor - by Gribouillis - Jan-08-2024, 10:29 PM
RE: question about descriptor - by akbarza - Jan-09-2024, 07:00 AM
RE: question about descriptor - by Gribouillis - Jan-09-2024, 07:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Type conversion issue while using Descriptor in Python3 mailnsuresh 1 2,915 May-07-2020, 12:26 PM
Last Post: deanhystad
  strange effect from duplicating a file descriptor Skaperen 1 2,096 Feb-18-2019, 08:20 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020