0

Somebody please help me to explain why there is no asset instant was passed in call func in python 3 and which annotation it was used in following lines of code:

class ValidateSetterProperty:
    def __init__(self, func):
        self.make_attr_dict()

    def __call__(self, *args, **kwargs):        
        print (args)
            
    def make_attr_dict(self):
        self.attr_dict = dict()          
        
class asset():
    def __init__(self):
        self.a = list()
        self.b = dict()
        self.c = list()

    @ValidateSetterProperty
    def __setattr__(self):
        pass
        
asset = asset()

Output:

Python 3.x

('a', [])
('b', {})
('c', [])

Python 2.x

(<__main__.asset instance at 0x7feeef8ae950>, 'a', [])
(<__main__.asset instance at 0x7feeef8ae950>, 'b', {})
(<__main__.asset instance at 0x7feeef8ae950>, 'c', [])
  • Because you need to implement the descriptor protocol to bind the instance as the first argument I'd you want functions decorated with your custom object to work like methods – juanpa.arrivillaga Sep 14 '20 at 04:27
  • But why python3 didn't automatically passes the class instance as reference like python2x ? Do you know how to implement the descriptor protocol. thank in advance – Nguyễn Hiệp Bình Sep 14 '20 at 04:41
  • Because you were using an old-style class in Python 2 since you didn't inherit from object, and they work differently, and they no longer exist in Python 3. As for descriptors, here is the [HOWTO](https://docs.python.org/3/howto/descriptor.html) – juanpa.arrivillaga Sep 14 '20 at 05:21
  • Would you please more specific ? Pls. I read the article but I still can't find any solution. I trying to migrate from python2 to python3 but I can't find the way to pass the instance to the __call__ func as arg like python2 – Nguyễn Hiệp Bình Sep 14 '20 at 07:48

0 Answers0