Re: Ordering attributes for dynamically generated class
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 fairwinds...@gmail.com 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 -- http://mail.python.org/mailman/listinfo/python-list
Ordering attributes for dynamically generated class
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Ordering attributes for dynamically generated class
On Jan 18, 9:52 am, David Pratt fairwinds...@gmail.com 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
Re: Ordering attributes for dynamically generated class
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 fairwinds...@gmail.com 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Ordering attributes for dynamically generated class
Aaron Brady castiro...@gmail.com wrote: On Jan 18, 9:52 am, David Pratt fairwinds...@gmail.com 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. You can certainly pass a subclass of 'dict' to type(), in fact in Python 3.0 you can use a metaclass with a __prepare__ method to substitute your own value for __dict__ in any class definition, but I don't think your own type will preserved when the instance is actually constructed. A simple solution which works in any version of Python is just to maintain an attribute with the desired ordering and override __dir__ to return the attributes in that order. -- http://mail.python.org/mailman/listinfo/python-list