What is the purpose of Zope Interfaces?

0 votes
I have recently started using Zope interfaces in my code and they are only documentation. I use them to specify what attributes the class should possess, explicitly implement them in the appropriate classes and explicitly check for them where I expect one. But What should I do if I would like them to do more such as verify that the class has implemented the interface, instead of just verifying that I have said that the class implements the interface. I have read the zope wiki a couple of times, but still cannot see much more use for interfaces than what I am currently doing.  What else can you use these interfaces for, and how do you use them for more?

Can anyone help me with this?
Feb 28 in Python by ana1504.k
• 7,590 points

1 answer to this question.

0 votes
You can actually test if your object or class implements your interface. For that you can use verify module.

For example:

>>> from zope.interface import Interface, Attribute, implements
>>> class IFoo(Interface):
...     x = Attribute("The X attribute")
...     y = Attribute("The Y attribute")

>>> class Foo(object):
...     implements(IFoo)
...     x = 1
...     def __init__(self):
...         self.y = 2

>>> from zope.interface.verify import verifyObject
>>> verifyObject(IFoo, Foo())

>>> from zope.interface.verify import verifyClass
>>> verifyClass(IFoo, Foo)
answered Feb 28 by SDeb
• 12,600 points

Related Questions In Python

0 votes
1 answer

What is the purpose of self?

The reason you need to use self. ...READ MORE

answered Aug 3, 2018 in Python by Priyaj
• 56,160 points
0 votes
0 answers

what is the purpose of the argument 'end' in the print function?

is it necessary to use this argument ...READ MORE

May 9 in Python by Waseem
• 3,600 points
0 votes
1 answer

What is the purpose of using lambda functions in Python?

The main purpose of anonymous functions come ...READ MORE

answered Jun 11 in Python by Nisa
• 1,090 points
0 votes
0 answers

What is the purpose of hash function in python?

Can you give an example? How is it ...READ MORE

Jul 3 in Python by Waseem
• 3,600 points
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 136 views
0 votes
1 answer

What is the purpose of inner class in Python?

Advantages of inner class: Logical grouping of classes: ...READ MORE

answered Feb 7 in Python by SDeb
• 12,600 points
0 votes
1 answer

Is arr.__len__() the preferred way to get the length of an array in Python?

my_list = [1,2,3,4,5,6,7] len(my_list) # 7 The same works for ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 12,600 points