English 中文(简体)
在 p创造接口和可互换的实施
原标题:Creating an interface and swappable implementations in python
  • 时间:2010-05-18 18:50:56
  •  标签:
  • python
  • oop

有可能在 p和接口的各种实施中建立一个班级接口。

例:我想为人群(和所有方法等)设置一个班级。 如果涉及商业部门,我想总结一下遵守合同的情况。

将来,如果我想使用另一个部分或我自己的法典,那么我就想能够把事情 out掉,而不要把事情 couple起来。

可能吗? I m New to python.

问题回答

对于那些来自强类语言背景的人来说,Sharma不需要一个班级接口。 您可以使用基类模拟。

class BaseAccess:
  def open(arg):
    raise NotImplementedError()

class Pop3Access(BaseAccess):
  def open(arg):
    ...

class AlternateAccess(BaseAccess):
  def open(arg):
    ...

但是,在不使用“基地”设施的情况下,你可以很容易地书写同样的法典。 严格分类的语文需要在汇编时间进行类型核对。 对沙尔而言,这并非必要,因为一切都处于动态时期。 页: 1

有一个摘要基类模块,在Stefan 2.6中添加。 但我没有这样做。

当然。 没有必要在这一情况下建立一个基类或接口,因为一切都是动态的。

一种选择是使用zope界面。 然而,正如Wai Yip Tung所述,你不需要利用接口来取得相同结果。

<代码>zope.interface的包裹实际上更是发现如何与物体互动的工具(一般是在有多个开发商的大代码基地之内)。

是的,这是可能的。 这样做通常没有任何障碍:仅仅保持一个稳定的促和反应机制,并改变你如何执行。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签