Can zope.interface determine what the __init__ class method should look like?

I have several similar classes that will all be initialized with the same code and, therefore, must have the same "constructor signature". (Are there really constructors and signatures in dynamic Python? I'm distracted.)

What is the best way to define __ init __ classes using zope.interface?

I will insert the code that I used to experiment with zope.interface to facilitate discussion:


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)

I can't just define the __ init __ function in ITest because it will be considered specifically by the Python interpreter - I think? However, this does not work. So, what is the best way to define a "class constructor" using zope.interface?

+3
3

: .

, , . , - , .

, , __init__ ! , ( Python). __init__ , , () __init__ ( ).

, , , , .

, . Python, . directlyProvides, , . @provider() classProvides moduleProvides , .

factory; - , , factory __call__, , . factory:

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

zope.component class , getInterfaces , . IFactory, __init__:

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)

factory zope.component, , ITestFactory.

zope.interface.directlyProvides , factory ITestFactory, zope.component.Factory IFactory.

+8

, __init__ :

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))

100%, . Python, - .:-) __init__ , , .

zope.interface - , . , , __init__, : " IMyFace", , , , , .

+2

What you ask does not make much sense. The interface file must contain a description of the interface, but not any specific implementation that needs to be called from any point at any point. What do you inherit. from a common base class. zope.interface is NOT about inheritance.

0
source

All Articles