Python static method is not always callable

后端 未结 2 844
长情又很酷
长情又很酷 2021-02-08 12:29

While parsing attributes using __dict__, my @staticmethod is not callable.

Python 2.7.5 (default, Aug 29 2016, 10:12:21)         


        
2条回答
  •  伪装坚强ぢ
    2021-02-08 12:45

    The reason for this behavior is the descriptor protocol. The C.foo won't return a staticmethod but a normal function while the 'foo' in __dict__ is a staticmethod (and staticmethod is a descriptor).

    In short C.foo isn't the same as C.__dict__['foo'] in this case - but rather C.__dict__['foo'].__get__(C) (see also the section in the documentation of the Data model on descriptors):

    >>> callable(C.__dict__['foo'].__get__(C))
    True
    >>> type(C.__dict__['foo'].__get__(C))
    function
    
    >>> callable(C.foo)
    True
    >>> type(C.foo)
    function
    
    >>> C.foo is C.__dict__['foo'].__get__(C)
    True
    

    In your case I would check for callables using getattr (which knows about descriptors and how to access them) instead of what is stored as value in the class __dict__:

    def bar(self):
        print('Is bar() callable?', callable(C.bar))
        print('Is foo() callable?', callable(C.foo))
        for attribute in C.__dict__.keys():
            if attribute[:2] != '__':
                value = getattr(C, attribute)
                print(attribute, '\t', callable(value), '\t', type(value))
    

    Which prints (on python-3.x):

    Is bar() callable? True
    Is foo() callable? True
    bar      True    
    foo      True    
    

    The types are different on python-2.x but the result of callable is the same:

    Is bar() callable? True
    Is foo() callable? True
    bar      True    
    foo      True    
    

提交回复
热议问题