[Zope] newbie - class attributes!

Casey Duncan casey@zope.com
Thu, 27 Mar 2003 09:55:36 -0500


Class attributes can be used in Zope just like in normal Python with one =
large=20
caveat: Changes to class attributes are not persisted. Therefore it is on=
ly=20
useful for constant values that are changed only at dev time.

To share values that change, you can use Zope's acquisition machinery. A=20
simple way to do this is to create a property in a folder above all of th=
e=20
instances of your class (like in the root folder). You can then acquire t=
his=20
property value like its an attribute of the object:

v =3D self.some_property_name

However, it would probably be better to have a sensible default such as:

try:
    v =3D self.some_property_name
except AttributeError:
    v =3D some_default

You can also be explicit in your code by declaring this value as acquired=
:

from Acquisition import Acquired

class Stuff(SimpleItem):

  some_propery_name =3D Acquired()

  def someMethod(self):
      try:
          v =3D self.some_property_name
      except AttributeError:
          v =3D some_default
      ...

Then at least you have some indication where "property_name" comes from i=
n=20
your code.

hth,

-Casey

On Thursday 27 March 2003 02:37 am, Danny wrote:
> Hi again
>=20
>=20
>=20
>=20
> Im doing some product-stuff and have reached a point where its=20
> necessary that som instances of the same product-type share a property=20
> (class attribute). How do I do this in Zope and Python?
>=20
>=20
>=20
>=20
> Regards
>=20
>=20
>=20
>=20
> Danny Nielsen
>=20
>=20
>=20
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://mail.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -=20
>  http://mail.zope.org/mailman/listinfo/zope-announce
>  http://mail.zope.org/mailman/listinfo/zope-dev )
>=20