Access to variable descriptor



  • Question of working with the descriptors. I cannot understand how to access self.dct in the code below:

    class C(object):
    
    def __init__(self,name,value=None):
        self.dct  = {}
        self.name = name
        self.value = value
    
    def __get__(self, instance, owner):
        if self.value:
            self.dct[self.name] = self.value
            self.value = None
        return self.dct[self.name]
    
    def __set__(self, instance, value):
        self.dct[self.name] = value
    
    def __delete__(self, instance):
        del self.dct[self.name]
    

    class A(object):
    c = C('c',10)

    z = A()
    print(z.c)

    Only if you get self.dct out. in Then dct is available via C.dct, but how do we call it if it's registered in in I can't understand.

    The same question is when I stand self.dct. in And I'm declaring it just like a dct. get Is it still available through self.dct?

    print(A.c.dct) #AttributeError: 'int' object has no attribute 'dct'
    print(z.c.dct) #AttributeError: 'int' object has no attribute 'dct'



  • If dct is initiated init, access may be obtained only after the creation of a copy of the class(s) as appropriate. And when dct is out of init, dct is still class (this and self.dct), only available through C.dct because it is initiated in the creation of a class, not a copy.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2