[Python-Dev] class_init (original) (raw)
Thomas Heller thomas.heller@ion-tof.com
Fri, 16 Nov 2001 17:56:34 +0100
- Previous message: [Python-Dev] __class_init__
- Next message: [Python-Dev] __class_init__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
From: "Tim Peters" <tim.one@home.com> [class_init methods]
I pinged Guido about this (he'll fire me if I ever do that again ), and he's really not keen on it. "The right way" is to define a custom metaclass instead (whose init plays the role classinit would have played if defined in the class). It makes sense to me, but I'll have to play with it to be convinced it's as usable.
It seems Guido is right ;-), it is easier than ever!
C:\sf\python\dist\src\PCbuild>python Python 2.2b1+ (#25, Nov 6 2001, 21🔞43) [MSC 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
class metaclass(type): ... def init(self, *args): ... type.init(self, *args) ... try: ... cls_init = getattr(self, 'init_class') ... except AttributeError: ... pass ... else: ... cls_init.im_func(self) ... meta = metaclass('meta', (), {}) class X(meta): ... def init_class(self): ... print "init_class", self ... init_class <class '__main__.X'> class Y(X): ... pass ... init_class <class '__main__.Y'> ^Z
Now I'll have to find out how to convert this to C.
Thomas
- Previous message: [Python-Dev] __class_init__
- Next message: [Python-Dev] __class_init__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]