Metaclass with name overloading.
Carlos Ribeiro
carribeiro at gmail.com
Mon Sep 27 08:59:23 EDT 2004
More information about the Python-list mailing list
Mon Sep 27 08:59:23 EDT 2004
- Previous message (by thread): Metaclass with name overloading.
- Next message (by thread): Metaclass with name overloading.
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 27 Sep 2004 13:33:33 +0200, Jacek Generowicz <jacek.generowicz at cern.ch> wrote: > <snip> > I would then like the dictionary received by OverloadingClass.__new__ > to look something like this: > > {'att': (1,3), > 'meth: (<function meth at 0x4018e56c>, <function meth at 0x4018e80c>) } > > IOW, each name bound in the class definition should have associated > with it, a tuple containing all the objects which were bound to that > name, rather merely keeping the most recent binding for any given > name. > > I was wondering whether it would be possible to achieve this by > forcing Python to use some dicitonary proxy (which accumulates values, > rather that keeping just the last value to be associated with a key), > instead of dict, when executing the class definiton? > > Is something like this at all possible in pure Python? or does in > require fiddling around in the guts of the parser? No, you can't, and it's not just a parser issue. Python uses direct C calls to the native dict type. It's hard coded, and I don't see any obvious or easy way to override it except by rewriting all such code. The metaclass receives the dictionary after all declarations were collected, and then it's too late to act upon it. Now that we're talking about it, I would like to discuss how this type of hack could possibly be done in a future version of Python (probaly Python 3.0, I don't think it's anywhere close to possible for Python 2.x). 1) One such idea is to provide the metaclass with a __dict__ factory. For example: class MyMetaclass(type): def __new__(...): ... def __dict__(cls): return CustomDict() ... where CustomDict is a user-defined mapping type that can store more information about the entries than the native dict: -- CustomDict[name] would retrieve a tuple containing all entries, in reverse order. CustomDict[name][0] would retrieve the last definition. -- The order of the definitions would be preserved, and the iterators (iterkeys, iteritems, itervalues) would all iterate over the entries in the order of the definition. By using a user-defined dict, we would still use the default dict most of the time without any noticeable performance hit, but would be able to change the guts of the class declaration system whenever needed. 2) Another (crazy) idea is to have the possibility to declare anonymous class members, such as in: class MyClass: """the first anonymous member is the doc string""" """the second anonymous member is __anon__[0]""" 1.5 # __anon__[1] = 1.5 By anonymous members, I mean anything that is not a def, a nested class, or a value that wasn't assigned or bound to name. That's would be nice to have too :-) ------- p.s. In the particular case of the original poster, I'm wondering what kind of application did he had in mind. I had similar needs while studying some alternatives to declare some types of data structure in Python -- forms, reports, webpages, etc -- stuff where the order of the entries is potentially as important than the actual member names. I'm really curious about it... -- Carlos Ribeiro Consultoria em Projetos blog: http://rascunhosrotos.blogspot.com blog: http://pythonnotes.blogspot.com mail: carribeiro at gmail.com mail: carribeiro at yahoo.com
- Previous message (by thread): Metaclass with name overloading.
- Next message (by thread): Metaclass with name overloading.
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list