Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Ordering attributes for dynamically generated class

Reply
Thread Tools

Ordering attributes for dynamically generated class

 
 
David Pratt
Guest
Posts: n/a
 
      01-18-2009
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
 
Reply With Quote
 
 
 
 
Aaron Brady
Guest
Posts: n/a
 
      01-18-2009
On Jan 18, 9:52*am, David Pratt <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
 
David Pratt
Guest
Posts: n/a
 
      01-18-2009
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


On Jan 18, 2009, at 11:57 AM, Aaron Brady wrote:

> On Jan 18, 9:52 am, David Pratt <(E-Mail Removed)> wrote:
>> 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.
> --
> http://mail.python.org/mailman/listinfo/python-list


 
Reply With Quote
 
David Pratt
Guest
Posts: n/a
 
      01-19-2009
Hi Aaron, this worked out fine. Using an ordered dict to subclass dict.

Many thanks.
David

On Jan 18, 2009, at 11:57 AM, Aaron Brady wrote:

> On Jan 18, 9:52 am, David Pratt <(E-Mail Removed)> wrote:
>> 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.
> --
> http://mail.python.org/mailman/listinfo/python-list


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Z-Ordering (Morton ordering) question nbigaouette C Programming 2 11-06-2009 05:26 AM
Ordering XML Attributes with Hpricot? Lance Pollard Ruby 7 09-25-2009 03:18 PM
Is it possible for me to get the class file of dynamically generated proxy class? billdavidcn@gmail.com Java 2 08-20-2007 03:05 AM
Builder gem - xml attributes ordering Arunprabu Durairaju Ruby 5 06-07-2007 05:50 PM
Dynamically create a class (or class instance) and its attributes? Robert Oschler Python 3 07-27-2004 02:48 PM



Advertisments