Re: [Tutor] Subclassing data attributes
Hi Kent, Kent Johnson wrote on 04.11.2005: > >OK I'll try again. I still don't understand why you don't like using >straight class attributes - is it to keep the size of the class >namespace smaller? > Yes, and to distinguish between different kinds of attributes in the main script. In the end, it is mainly a matter of readability in the main script. I am beginning to suspect that I trade in too much simplicity in my Data module. >I'm also not sure if you want to be able to access Base.children and >Page.children from a subclass of Page. If not, then you can use a >hierarchical dictionary to do what you want. How about this? > Your UserDict solution looks good. Thank you! >PS Please reply on list. > I usually do, sorry. - Jan -- Hanlon's Razor: Never attribute to malice that which can be adequately explained by stupidity. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Subclassing data attributes
Jan Eden wrote: > Not exactly. My current setup bundles all data attributes in a single > module Data containing a single class for each object. So in my main > module (Show), I define:> > class Page(Data.Page): > ... > > and Data contains: > > class Base: > children = 'xyz' > children_query = 'SELECT...' > > class Page: > children = 'something' > own_type = 'somethingelse' > populate_query = "SELECT ..." > index_link = "..." > ... > > So I can use self.children_query for an object of class Show.Page to > get the data attribute defined in class Data.Base. > > But since there are many data attributes, I'd like to use things like: > > self.templates['index_link'] > self.templates['child_link'] > self.queries['populate'] > > or > > self.Templates.index_link > self.Templates.child_link > self.Queries.populate OK I'll try again. I still don't understand why you don't like using straight class attributes - is it to keep the size of the class namespace smaller? I'm also not sure if you want to be able to access Base.children and Page.children from a subclass of Page. If not, then you can use a hierarchical dictionary to do what you want. How about this? import UserDict class ChainDict(UserDict.DictMixin): ''' A dict that will delegate failed lookups to a parent ''' def __init__(self, parent=None): self._dict = dict() self._parent = parent # __setitem__() and __delitem__() delegate to self._dict self.__setitem__ = self._dict.__setitem__ self.__delitem__ = self._dict.__delitem__ def __getitem__(self, key): try: return self._dict[key] except KeyError: if self._parent is not None: return self._parent[key] def keys(self): keys = self._dict.keys() if self._parent is not None: keys.extend(self._parent.keys()) class Base: templates = dict() templates['children'] = 'xyz' templates['children_query'] = 'SELECT * from Base' class Page(Base): templates = ChainDict(Base.templates) templates['children'] = 'something' templates['own_type'] = 'somethingelse' templates['populate_query'] = "SELECT * from Page" templates['index_link'] = "..." b = Base() print b.templates['children'] print b.templates['children_query'] print p = Page() print p.templates['children'] print p.templates['children_query'] print p.templates['index_link'] Kent PS Please reply on list. -- http://www.kentsjohnson.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Subclassing data attributes
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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Subclassing data attributes
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'. 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? Thanks, Jan -- I'd never join any club that would have the likes of me as a member. - Groucho Marx ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor