[Tutor] Subclassing data attributes

Jan Eden lists at janeden.org
Thu Nov 3 13:22:27 CET 2005


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'.

So I thought about realizing this with classes:

class A:
    class Templates:
        attr1 = 'string'
The problem here is that once an object uses class C.Templates to lookup attr3, it will not consult class A.Templates for attr1.

How can I avoid a complete inheritance mess and structure all my data attributes at the same time?


I'd never join any club that would have the likes of me as a member. - Groucho Marx

More information about the Tutor mailing list