Using property() on classmethods

后端 未结 15 954
Happy的楠姐
Happy的楠姐 2020-11-22 16:55

I have a class with two class methods (using the classmethod() function) for getting and setting what is essentially a static variable. I tried to use the property() functi

15条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 17:52

    After searching different places, I found a method to define a classproperty valid with Python 2 and 3.

    from future.utils import with_metaclass
    
    class BuilderMetaClass(type):
        @property
        def load_namespaces(self):
            return (self.__sourcepath__)
    
    class BuilderMixin(with_metaclass(BuilderMetaClass, object)):
        __sourcepath__ = 'sp'        
    
    print(BuilderMixin.load_namespaces)
    

    Hope this can help somebody :)

提交回复
热议问题