Python Forum
problem descriptors in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem descriptors in Python
#1
hi
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 updated
please 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
Reply


Messages In This Thread
problem descriptors in Python - by akbarza - Dec-26-2023, 11:13 AM
RE: problem descriptors in Python - by deanhystad - Dec-26-2023, 03:43 PM
RE: problem descriptors in Python - by akbarza - Dec-27-2023, 06:44 AM
RE: problem descriptors in Python - by akbarza - Dec-27-2023, 07:03 AM
RE: problem descriptors in Python - by Gribouillis - Dec-26-2023, 04:15 PM
RE: problem descriptors in Python - by akbarza - Dec-27-2023, 07:20 AM
RE: problem descriptors in Python - by Gribouillis - Dec-27-2023, 07:56 AM
RE: problem descriptors in Python - by akbarza - Dec-27-2023, 01:22 PM
RE: problem descriptors in Python - by deanhystad - Dec-27-2023, 09:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  does open() still take file descriptors in py3 Skaperen 2 4,165 Jan-25-2017, 02:30 AM
Last Post: Skaperen
  file descriptors Skaperen 7 8,638 Jan-15-2017, 09:18 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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