English 中文(简体)
是否应当研究?
原标题:Can zope.interface define how a class __init__ method should look?
  • 时间:2011-03-19 22:26:55
  •  标签:
  • python
  • zope

我有几门类似的班级,所有这些班级都将由相同的代码开始,因此需要有相同的“施工签字”。 (在动态的沙捞越中是否有真正的构造和签名?) 页: 1

如何利用同位素确定哪类------参数?

I ll Paste somecode I ve used for Trialing zope. 3. 促进讨论的接口:


from zope.interface import Interface, Attribute, implements, verify

class ITest(Interface):
    required_attribute = Attribute(
        """A required attribute for classes implementing this interface.""")
    def required_method():
        """A required method for classes implementing this interface."""

class Test(object):
    implements(ITest)
    required_attribute = None
    def required_method():
        pass

print verify.verifyObject(ITest, Test())
print verify.verifyClass(ITest, Test)

我只能界定《禁试》中的“......”功能,因为这种功能将特别由斯堪的译员处理——我想? 不管怎样,它似乎都没有工作。 因此,用同位素界定“级建筑商”的最佳方式是什么?

最佳回答

First of all: there is a big difference between the concepts of providing and implementing an interface.

基本上,“implement一个接口”,即provide这一接口。 毕竟,这些课程是实例的蓝图,详细说明其执行情况。

现在,一个接口描述了根据具体情况提供的执行情况,但<代码>_init__。 方法不是例子之一! 它是各年级直接提供的接口的一部分(代之以“灰色”术语中的一种分类方法)。 如果您界定了<代码>_init__。 您的接口方法,您宣布,您的事例(<>provide)为_init__,s well(作为实例方法)。

So interfaces describe what kind of instances you get, not how you get them.

Now, interfaces can be used for more than just describing what functionality an instance provides. You can also use interfaces for any kind object in Python, including modules and classes. You ll have to use the directlyProvides method to assign an interface to these, as you won t be calling these to create an instance. You can also use the @provider() class decorator, or the classProvides or moduleProvides functions from within a class or module declaration to get the same results.

在此情况下,你想要的是工厂的定义;工厂是工厂,一旦被叫作,就会产生一种情况,因此工厂的界面必须提供<代码>->->->-方法,以表明这些方法是可以使用的。 您树立的榜样是:

from zope import interface

class ITest(interface.Interface):
    required_attribute = interface.Attribute(
        """A required attribute for classes implementing this interface.""")
    def required_method():
        """A required method for classes implementing this interface."""

class ITestFactory(interface.Interface):
    """Creates objects providing the ITest interface"""
    def __call__(a, b):
        """Takes two parameters"""

@interface.implementer(ITest)
@interface.provider(ITestFactory)
class Test(object):
    def __init__(self, a, b):
        self.required_attribute = a*b

    def required_method():
        return self.required_attribute

The zope.component package provides you with a convenience class and interface for factories, adding a getInterfaces method and a title and description to make discovery and introspection a little easier. You can then just subclass the IFactory interface to document your __init__ parameters a little better:

from zope import component

class ITestFactory(component.interfaces.IFactory):
    """Creates objects providing the ITest interface"""
    def __call__(a, b):
        """Takes two parameters"""

testFactory = component.Factory(Test,  ITest Factory , ITestFactory.__doc__)
interface.directlyProvides(testFactory, ITestFactory)

现在,你可以将工厂登记为<代码>zope.component效用,例如,允许其他代码找到所有国际试验公司。

I used zope.interface.directlyProvides http://www.ohchr.org。 通常只有<代码>tactory接口。

问题回答

不适用。

from zope.interface import Interface, Attribute, implements, verify

class ITest(Interface):
    required_attribute = Attribute(
        """A required attribute for classes implementing this interface.""")
    def __init__(a,b):
        """Takes two parameters"""
    def required_method():
        """A required method for classes implementing this interface."""

class Test(object):
    implements(ITest)
    def __init__(self, a, b):
        self.required_attribute = a*b
    def required_method():
        return self.required_attribute

print verify.verifyClass(ITest, Test)
print verify.verifyObject(ITest, Test(2,3))

我不相信你会问什么。 如果你想在沙捞越几级有同样的构造签名,那么,这样做的唯一途径是实际上,在这些类别的同一构造上签字。 如果您通过次级或采用不同的<代码>_init_,就每一类而言,只要有相同签名,就算不成问题。

zope.interface is not about defining methods, but declaring signatures. You can therefore define an interface that has a specific signature, also on the __init__, but this is just saying "This object implements the signature IMyFace", but saying that a class implements an interface will not actually make the class implement the interface. You still need to implement it.

Does not make much sense what you are asking. The interface file is supposed to keep the interface description but not any specific implementation to be called from some where at any point. What you what is to inherit. from a common base class. zope.interface is NOT about inheritance.





相关问题
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 ]="...

热门标签