[Tutor] Subclassing data attributes

Kent Johnson kent37 at tds.net
Thu Nov 3 14:09:35 CET 2005


Jan Eden wrote:
> Hi,
> 
> the module Data.py stores a number of data attributes. I'd like to structure the storage of these attributes, either in subclasses or in dictionaries.
> 
> My first attempt was this:
> 
> class A:
>     templates['attr1'] = 'string'
>     queries['children'] = ...
>     
> class B(A):
>     templates['attr2'] = 4
>     queries['children'] = ...
>     
> class C(B):
>     templates['attr3'] = 939.121
>     queries['populate'] = ...
>     
> Python obviously complains about unknown names 'templates' and 'queries'.

I'm not at all clear why you want to do this. Can you give an example of how you would use these classes?

If you want the classes to be containers for data you can just use class attributes like this:
 >>> class A(object):
 ...   attr1 = 'string'
 ...   children = 'children of A'
 ...
 >>> class B(A):
 ...   attr2 = 4
 ...   children = 'children of B'
 ...
 >>> A.attr1
'string'
 >>> A.children
'children of A'
 >>> B.attr1
'string'
 >>> B.attr2
4
 >>> B.children
'children of B'

You might also be interested in this recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/305268

Is either of these close to what you want?

Kent

-- 
http://www.kentsjohnson.com



More information about the Tutor mailing list