The following quote is taken from lstaffor's ZClass Properties HOW-TO: http://www.zope.org/Members/lstaffor/zProperties ----- In the field of object-orientented programming, the terms "class variables" and "instance variables" are often used for the data structures that are common to all methods of a class or of an object that is an instance of that class. ... For example, you may want a given data item to be a constant that is shared by all instances; the constant can be changed only by changing the class definition. ---- I'm confused about how class variables are implemented in Zope. For example, let's say I have a ZClass with instance variable "Subject" which is a list of keys into a dictionary "SubjectChoices". Rather than every instance maintaining a separate copy, SubjectChoices should be a class variable - all instances see the same dictionary. Any insights would be most appreciated. Cheers, Darran. -- Darran Edmundson [Darran.Edmundson@anu.edu.au]
Darran Edmundson wrote:
(snip)
I'm confused about how class variables are implemented in Zope.
I prefer to call these shared instance attributes.
For example, let's say I have a ZClass with instance variable "Subject" which is a list of keys into a dictionary "SubjectChoices". Rather than every instance maintaining a separate copy, SubjectChoices should be a class variable - all instances see the same dictionary. Any insights would be most appreciated.
You simply have to arrange to get SubjectChoices to be an attribute of the class managed by the ZClasses. If there was a way to create dictionary properties, you could do this via a propertysheet, since ZClass propertysheets create default property values in classes. Since Zope doesn't give you a way to spell this through the web, you have to resort to Python. You could define a Python mix-in class that has the property: class MixIn: SubjectChoices={...} and mix it into your ZClass. Alternatively, you could, from Python (either using the Python interactive interpreter or an external method do something like: import Zope app=Zope.app() app.Control_Panel.yourProduct.yourZClass.setClassAttr( 'SubjectChoices',{...}) get_transaction().commit() app._p_jar.close() Jim -- Jim Fulton mailto:jim@digicool.com Technical Director (888) 344-4332 Python Powered! Digital Creations http://www.digicool.com http://www.python.org Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email address may not be added to any commercial mail list with out my permission. Violation of my privacy with advertising or SPAM will result in a suit for a MINIMUM of $500 damages/incident, $1500 for repeats.
Jim, pardon my denseness but I'm still struggling with this. It seems like your response contains conflicting ideas.
You simply have to arrange to get SubjectChoices to be an attribute of the class managed by the ZClasses.
import Zope app=Zope.app() app.Control_Panel.yourProduct.yourZClass.setClassAttr( 'SubjectChoices',{...}) get_transaction().commit() app._p_jar.close()
I understand the sentence above and the code snippet to mean the same. Namely, that *if* MyProduct.MyZClass had a properties tag, I could simply use this to add a new property which all instances of MyZClass would acquire. ZClasses don't have property forms and so I use the python code to add the desired attribute. However, ...
If there was a way to create dictionary properties, you could do this via a propertysheet, since ZClass propertysheets create default property values in classes. Since Zope doesn't give you a way to spell this through the web, you have to resort to Python.
If I add a ClassVariables propertysheet to MyZClass, won't this get instantiated every time I create a new MyZClass instance? It seems to me that "class variables" cannot be added to a ZClass via the web interface, not just for dictionaries (since this isn't a recognized Zope property type), but for all property types. Cheers, Darran. -- Darran Edmundson [Darran.Edmundson@anu.edu.au]
Darran, The way I understand it, Zope's implementation of properties allows you to use them as class constants, class variables, instance constants, or instance variables depending on what methods you create to modify the properties and how and when you use (or permit others to use) those methods. - Class constants are properties that have no methods to change them (aside from the class Property Sheets tab). - Class variables (now that I think about it, I can't see how to implement them). - Instance constants are properties that are assigned values only by the class_add constructor. - Instance variables are properties that are assigned values by methods of the class. I don't understand how it's implemented in Zope, but I'd guess that all Zope does is give the instance it's private copy of the property if and only if the instance has modified the property, otherwise give the instance the value of the class property. Maybe Jim will step in again and correct or clarify. -- Loren
-----Original Message----- From: dee124@rsphy1.anu.edu.au [mailto:dee124@rsphy1.anu.edu.au]On Behalf Of Darran Edmundson Sent: Thursday, December 02, 1999 21:12 To: zope@zope.org Subject: Re: [Zope] Class variables in ZClasses
Jim, pardon my denseness but I'm still struggling with this. It seems like your response contains conflicting ideas.
You simply have to arrange to get SubjectChoices to be an attribute of the class managed by the ZClasses.
import Zope app=Zope.app() app.Control_Panel.yourProduct.yourZClass.setClassAttr( 'SubjectChoices',{...}) get_transaction().commit() app._p_jar.close()
I understand the sentence above and the code snippet to mean the same. Namely, that *if* MyProduct.MyZClass had a properties tag, I could simply use this to add a new property which all instances of MyZClass would acquire. ZClasses don't have property forms and so I use the python code to add the desired attribute. However, ...
If there was a way to create dictionary properties, you could do this via a propertysheet, since ZClass propertysheets create default property values in classes. Since Zope doesn't give you a way to spell this through the web, you have to resort to Python.
If I add a ClassVariables propertysheet to MyZClass, won't this get instantiated every time I create a new MyZClass instance?
It seems to me that "class variables" cannot be added to a ZClass via the web interface, not just for dictionaries (since this isn't a recognized Zope property type), but for all property types.
Cheers, Darran.
-- Darran Edmundson [Darran.Edmundson@anu.edu.au]
participants (3)
-
Darran Edmundson -
Jim Fulton -
Loren Stafford