metaclass : parse all class once before doing anything else ?

Paddy paddy3118 at netscape.net
Sat Jul 29 06:51:22 EDT 2006


Laurent Rahuel wrote:
> Hi,
>
> I have a much to smart problem for my brain.
>
> Here is the deal :
>
> I got a metaclass named Foo
>
> Then I got two others classes:
>
> class Bar(Foo):
>         pass
>
> class Baz(Foo):
>         pass
>
> I know how to add some attrs, methods to Bar and Baz when the module is
> loaded but I need to do something more :
>
> Before adding anything to these classes,
> 1 - I need to parse all defined Foo classes
> 2 - "sort" them
> 3 - parse this sorted list to add attrs and methods.
>
> This seems to be really to clever for me ;-(
>
> Any idea ?
>
> Regards,
>
> Laurent.

I, like Diez am unsure of why you would need what you have asked for,
but maybe this will help.

You can keep  track of all instances of a class by this kind of thing:

>>> class C1(object):
... 	inst = []
... 	def __init__(self):
... 		self.inst.append(self)
...
>>> i1 = C1()
>>> i2 = C1()
>>> print i1,i2
<__main__.C1 object at 0x0128C970> <__main__.C1 object at 0x0128CA50>
>>> print C1.inst
[<__main__.C1 object at 0x0128C970>, <__main__.C1 object at
0x0128CA50>]
>>>




More information about the Python-list mailing list