[Zope3-Users] property in view/adapter

Roger Ineichen dev at projekt01.ch
Wed May 11 03:19:21 EDT 2005


Hi Marc

From: zope3-users-bounces at zope.org 
> [mailto:zope3-users-bounces at zope.org] On Behalf Of Marc Rijken
> Sent: Wednesday, May 11, 2005 1:12 AM
> To: zope3-users at zope.org
> Subject: [Zope3-Users] property in view/adapter
> 
> Hi Everybody,
> 
> I have an adapter Person to Object. Some code:
> 
> class Object:
>      def __init__(self):
>          self.attributes = {}
> 
> class NamedObject:
>      def __init__(self, context):
>          self.context = context
> 
>      def getName(self, type):
>          return self.context.attributes[type]
> 
>      def setName(self, type, value):
>          self.context.attributes[type] = value
> 
> def nameProperty(type):
>      fget = lambda self: self.getName(type)
>      fset = lambda self, value: self.setName(type, value)
>      return property(fget, fset)

In nameProperty(type, value) is the value missing.

> class Person(NamedObject):
> 
>      firstname = nameProperty('firstname')

Uh, that's not this easy. Take a look at how we use the property
A property is used like:

attributeName = property(getterMethod, setterMethod)

You try to define a own property I guess. You can do this
but I recommend to use the standard property.
You can find different samples in Zope3. 
If not, I can send you a sample.

> When I try in a view on an Object adapted to Person, the following:
> 
> self.context.firstname = 'Marc'
> 
> the name will *not* be set and I get no single error. When I call
> 
> self.context.setName('firstname', 'Marc')
> 
> the name will be set.
> 
> The getter (x=self.context.firstname) is functioning without 
> any problems.
> 
> It looks like property can not be used as setter in 
> views/adapters. Is 
> that the problem? Or is there an other problem?
> 
> Marc

Regards
Roger Ineichen 



More information about the Zope3-users mailing list