How to find the parent of an old-style class?

J

Jasper

I'm stuck using a library based on old style classes, and need to find
a class's parent at runtime.

With new style classes you can use .__base__ to inspect a parent, but
I can't remember how this was done in days of yore, before object.
I've tried googling, but apparently my search term Fu is weak. :-(

Can anyone help me out here? There must be something simple.

-Jasper
 
J

Jonathan Gardner

I'm stuck using a library based on old style classes, and need to find
a class's parent at runtime.

With new style classes you can use .__base__ to inspect a parent, but
I can't remember how this was done in days of yore, before object.
I've tried googling, but apparently my search term Fu is weak. :-(

Can anyone help me out here?  There must be something simple.

It's very odd that you need to know a class's parent. I'm interested
in hearing why you need this. In all my years, I've never had to do
this.

Regardless, I believe you are looking for __bases__.
 
J

Jasper

It's very odd that you need to know a class's parent. I'm interested
in hearing why you need this. In all my years, I've never had to do
this.

Regardless, I believe you are looking for __bases__.

*smack* That's what I get for programming too late into the morning;
can't believe I missed that. Thanks for sorting me out!

-Jasper


PS I'm using a hierarchy of (never instantiated) classes as resource
types in an economic sim, e.g. Resource, Luxury( Resource ),
Gold( Luxury ). Prices are dicts of {resource:amount}, where resource
can be something concrete like Gold, or a general group like Luxury
(which could be paid with Gold, but also Ivory, etc). Payments are in
dicts of {concrete-resource:amount}, and to verify correct payment I
iterate through them, matching concrete resources (via their parent)
to general price requirements like Luxury or Resource. I could avoid
referencing __bases__ by instead iterating through price and checking
issubclass(), but the logic is more complex.

And yes, I know this is a bit of an unorthodox use of classes. ;-)
 

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,774
Messages
2,569,598
Members
45,145
Latest member
web3PRAgeency
Top