Re: [Zope3-Users] property in view/adapter

2005-05-11 Thread Benji York
Marc Rijken wrote:
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)
class Person(NamedObject):
firstname = nameProperty('firstname')

It looks like property can not be used as setter in views/adapters. Is 
that the problem? Or is there an other problem?
It doesn't have anything to do with views or adapters, or even
Zope. :)
Properties only work properly on new style classes.  Change the line 
"class NamedObject:" to "class NamedObject(object):" and it will work.
--
Benji York
Sr. Software Engineer
Zope Corporation
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


RE: [Zope3-Users] property in view/adapter

2005-05-11 Thread Roger Ineichen
Hi Marc

From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Marc Rijken
> Sent: Wednesday, May 11, 2005 1:12 AM
> To: zope3-users@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 

___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users