[Tutor] Problem appending to a list using a property within a class
Kent Johnson
kent37 at tds.net
Wed Nov 9 11:55:54 CET 2005
John Fouhy wrote:
> On 09/11/05, Roy Bleasdale <royb at ubiquity.com.au> wrote:
>
>>I have created a List within a class. If I try and append to the list using
>>the set function using the property value my variable stops being a list.
>
> Properties only work properly with new style classes. You get a new
> style class by inheriting from another new style class, or by
> inheriting from object.
>
> Try changing the first line to "class ShoppingBag(object):".
Also you should use different names for the instance attributes (e.g. self.fruit) and the class properties (ShoppingBag.fruit). The usual convention is to prefix the attribute names with an underscore (self._fruit). If you use the same name the instance attribute will not be accessible - the property has priority (unlike most attribute lookups where an instance attribute will be found first).
Kent
--
http://www.kentsjohnson.com
More information about the Tutor
mailing list