Abstract attribute (not property)?

前端 未结 7 926
心在旅途
心在旅途 2020-11-30 22:41

What\'s the best practice to define an abstract instance attribute, but not as a property?

I would like to write something like:

class AbstractFoo(me         


        
相关标签:
7条回答
  • 2020-11-30 23:28

    Following https://docs.python.org/2/library/abc.html you could do something like this in Python 2.7:

    from abc import ABCMeta, abstractproperty
    
    
    class Test(object):
        __metaclass__ = ABCMeta
    
        @abstractproperty
        def test(self): yield None
    
        def get_test(self):
            return self.test
    
    
    class TestChild(Test):
    
        test = None
    
        def __init__(self, var):
            self.test = var
    
    
    a = TestChild('test')
    print(a.get_test())
    
    0 讨论(0)
提交回复
热议问题