Ordering attributes for dynamically generated class

D

David Pratt

Hi list. I use 'type' to generate classes but have a need to order
the attributes for the generated class. Of course a dict is not going
to maintain ordering. Is there any way to dynamically generate a
class with attributes in specific order?

my_new_class = type( 'MyNewClass', tuple_of_bases, dict_of_attributes)

Many thanks,
David
 
A

Aaron Brady

Hi list. I use 'type' to generate classes but have a need to order  
the attributes for the generated class. Of course a dict is not going  
to maintain ordering. Is there any way to dynamically generate a  
class with attributes in specific order?

my_new_class = type( 'MyNewClass', tuple_of_bases, dict_of_attributes)

Many thanks,
David

Just a thought, you can subclass 'dict' and assign an instance of it
to the __dict__ member of your new instance.
 
D

David Pratt

Hi Aaron. Yeah, definitely sounds like a possibility. I was able to
locate an ordered dict implementation that subclasses dict. This
might work fine. Might be able to pass into type method directly
since I think that dict passed into type is setting __dict__ I
believe. Let you know if that works out. Many thanks.

Regards,
David
 
D

David Pratt

Hi Aaron, this worked out fine. Using an ordered dict to subclass dict.

Many thanks.
David
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top