小编mor*_*uer的帖子

abc.abstractmethod +属性

根据文档,它应该可以结合使用@property@abc.abstractmethod因此以下内容应在python3.3中起作用:

import abc

class FooBase(metaclass=abc.ABCMeta):

    @property
    @abc.abstractmethod
    def greet(self):
        """ must be implemented in order to instantiate """
        pass

    @property
    def greet_comparison(self):
        """ must be implemented in order to instantiate """
        return 'hello'

class Foo(FooBase):
    def greet(self):
        return 'hello'
Run Code Online (Sandbox Code Playgroud)

测试实现:

In [6]: foo = Foo()
In [7]: foo.greet
Out[7]: <bound method Foo.greet of <__main__.Foo object at 0x7f935a971f10>>

In [8]: foo.greet()
Out[8]: 'hello'
Run Code Online (Sandbox Code Playgroud)

所以它显然不是属性,因为它应该这样工作:

In [9]: foo.greet_comparison
Out[9]: 'hello'
Run Code Online (Sandbox Code Playgroud)

也许我很愚蠢,或者根本不起作用,有人有主意吗?

python properties abc

3
推荐指数
1
解决办法
1448
查看次数

标签 统计

abc ×1

properties ×1

python ×1