[Zope-dev] [BlueBream] Referring to same interface using zope.schema.Object

Laurence Rowe l at lrowe.co.uk
Fri Jul 22 08:57:14 EDT 2011


On 22 July 2011 13:32, Joshua Immanuel <josh at hipro.co.in> wrote:
> Hello,
>
> On Fri, 2011-07-22 at 13:41 +0200, Jacob Holm wrote:
>> On 2011-07-22 13:26, Brian Sutherland wrote:
>> > This would be my first guess:
>> >
>> >     class INode(Interface):
>> >         pass
>> >
>> >     INode.parent = Object(
>> >             title=u"Parent node",
>> >             schema=INode
>> >             )
>> >
>> >     INode.children = List(
>> >             title=u'Child nodes',
>> >             value_type=Object(schema=INode)
>> >             )
>> >
>>
>
> The method suggested by Brian works without any issues. :)

No, there are issues. Take this example:

>>> class ITest(Interface):
...  title = schema.TextLine()
...
>>> ITest.names
<bound method InterfaceClass.names of <InterfaceClass __main__.ITest>>
>>> ITest.names()
['title']
>>> ITest.description = schema.Text()
>>> ITest.names()
['title']

The fields on an interface are not stored as attributes, but are
accessible using item access, e.g: ITest['title']. You cannot assign
that way though:

>>> ITest['description'] = schema.TextLine()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: 'InterfaceClass' object does not support item assignment

Adding to an interface requires messing with the
'_InterfaceClass__attrs' attribute of the dictionary and is
discouraged.

Laurence


More information about the Zope-Dev mailing list