Dec-26-2023, 11:13 AM
hi
the below code is in address:
https://docs.python.org/3/howto/descriptor.html
in the above address, it is quoted that:
how can I change the __get__ method to show(print at output) what is self and what is obj and what is objtype,namely for s=directory(..), output shows size and s and Directory
I read the first section of the above address, but I did not understand it. can you explain descriptors in Python?
thanks
the below code is in address:
https://docs.python.org/3/howto/descriptor.html
import os class DirectorySize: def __get__(self, obj, objtype=None): return len(os.listdir(obj.dirname)) class Directory: size = DirectorySize() # Descriptor instance def __init__(self, dirname): self.dirname = dirname # Regular instance attribute s = Directory('songs') g = Directory('games') s.size # The songs directory has twenty files g.size # The games directory has three files os.remove('games/chess') # Delete a game g.size # File count is automatically updatedplease note that you must change songs and games to two folders names in your system.
in the above address, it is quoted that:
Quote:Besides showing how descriptors can run computations, this example also reveals the purpose of the parameters to __get__(). The self parameter is size, an instance of DirectorySize. The obj parameter is either g or s, an instance of Directory. It is the obj parameter that lets the __get__() method learn the target directory. The objtype parameter is the class Directory.
how can I change the __get__ method to show(print at output) what is self and what is obj and what is objtype,namely for s=directory(..), output shows size and s and Directory
I read the first section of the above address, but I did not understand it. can you explain descriptors in Python?
thanks