metaclass : parse all class once before doing anything else ?

L

Laurent Rahuel

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

Diez B. Roggisch

Laurent said:
I got a metaclass named Foo


I have the impression that you are not talking about a meta-class, but a
normal class here.
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 ?

Why do you want to do this? It is in that way not possible - python has
no preprocessing step that would allow this.

I suggest you explain to us what you are after here, and then we might
come up with a solution.

Diez
 
P

Paddy

Laurent said:
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:
.... inst = []
.... def __init__(self):
.... self.inst.append(self)
....
 
G

gene tani

Paddy said:
Laurent said:

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:
... inst = []
... def __init__(self):
... self.inst.append(self)
...

or list all subclasses with

print LaurentsPutativeMetaClass.__subclasses__()

me 3 for: not clear what you're getting at
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top