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

Laurence Rowe <[email protected]>
Newsgroups gmane.comp.web.zope.bluebream,gmane.comp.web.zope.devel
Message-ID <CAOycyLQkV0Dm5c6ms=xa04zyT_JLzEgb+OfjBriKjUdKkTqCGA@mail.gmail.com>
On 22 July 2011 13:32, Joshua Immanuel <[email protected]> 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.