[Zope3-Users] How to persist an attribute of list type

Hermann Himmelbauer dusty at qwer.tk
Sun Dec 2 17:01:17 EST 2007


Am Freitag, 30. November 2007 19:34 schrieb Yuan HOng:
> Hi,
>
> It seems that I can not get list attribute of an persistent object
> changed. I have the following class, which has a list as one of its
> attributes:
>
> class Cart(Persistent):
>     items = []
>     amount = 0

Hehe, I had the same pitfall some weeks ago...

This has nothing to do with persitence, the problem is that you are assigning 
the dictionary to the class and not to the class instance (= the object). For 
that reason, strange things will happen, e.g.:

c1=Cart()
c2 = Cart()
c1.items.append("abc")
c2.items
['abc']

So, I follow this pattern:

class Cart(Persistent):
  items = None
  def __init__(self):
    self.items = []

My problem was that I was so much focussed on Persistence, that I forgot about 
this quite simple Python issue...

;-)

Best Regards,
Hermann

-- 
x1 at aon.at
GPG key ID: 299893C7 (on keyservers)
FP: 0124 2584 8809 EF2A DBF9  4902 64B4 D16B 2998 93C7


More information about the Zope3-users mailing list