Jas*_*ker 16 python documentation coding-style interface
因此,我在静态土地上花了大量时间后开始使用Python.我已经看到一些项目使得"接口"实际上只是没有任何实现的类.之前,我嘲笑这个想法,忽略那些项目的那一部分.但现在,我开始热衷于这个想法了.
我们很清楚,Python中的界面看起来像这样:
class ISomething(object):
def some_method():
pass
def some_other_method(some_argument):
pass
Run Code Online (Sandbox Code Playgroud)
请注意,您没有将self传递给任何方法,因此要求重写该方法以进行调用.我认为这是一种很好的文档和完整性测试形式.
那么每个人对这个想法的看法是什么?我做过的所有C#编程都被洗脑了,还是这个好主意?
Ken*_*Ken 29
我不确定这是什么意思.接口(无论如何这种形式)主要是为了解决缺少多重继承的问题.但是Python有MI,所以为什么不做一个抽象类呢?
class Something(object):
def some_method(self):
raise NotImplementedError()
def some_other_method(self, some_argument):
raise NotImplementedError()
Run Code Online (Sandbox Code Playgroud)
Tri*_*ych 10
pythonic方式是"请求宽恕而不是获得许可".接口是所有有关接收到的权限对象上执行一些操作.Python更喜欢这个:
def quacker(duck):
try:
duck.quack():
except AttributeError:
raise ThisAintADuckException
Run Code Online (Sandbox Code Playgroud)
我认为接口不会在代码环境中添加任何内容.
Foo并且有方法bar(),那么它就会抛出一个AttributeError.| 归档时间: |
|
| 查看次数: |
3230 次 |
| 最近记录: |