Ordering attributes for dynamically generated class

Discussion in 'Python' started by David Pratt, Jan 18, 2009.

  1. David Pratt

    David Pratt Guest

    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
    David Pratt, Jan 18, 2009
    #1
    1. Advertising

  2. David Pratt

    Aaron Brady Guest

    On Jan 18, 9:52 am, David Pratt <> 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.
    Aaron Brady, Jan 18, 2009
    #2
    1. Advertising

  3. David Pratt

    David Pratt Guest

    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 <> 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
    David Pratt, Jan 18, 2009
    #3
  4. David Pratt

    David Pratt Guest

    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 <> 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
    David Pratt, Jan 19, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Oschler
    Replies:
    3
    Views:
    476
    Christopher T King
    Jul 27, 2004
  2. Replies:
    2
    Views:
    309
    Twisted
    Aug 20, 2007
  3. nbigaouette

    Z-Ordering (Morton ordering) question

    nbigaouette, Nov 5, 2009, in forum: C Programming
    Replies:
    2
    Views:
    2,151
  4. Arunprabu Durairaju

    Builder gem - xml attributes ordering

    Arunprabu Durairaju, Jun 7, 2007, in forum: Ruby
    Replies:
    5
    Views:
    181
    Keith Fahlgren
    Jun 7, 2007
  5. Lance Pollard

    Ordering XML Attributes with Hpricot?

    Lance Pollard, Sep 24, 2009, in forum: Ruby
    Replies:
    7
    Views:
    121
    Lance Pollard
    Sep 25, 2009
Loading...

Share This Page