[Python-Dev] Classes with ordered namespaces
ericsnowcurrently at gmail.com
Thu Jun 27 23:48:25 CEST 2013
On Thu, Jun 27, 2013 at 9:35 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> On 06/27/2013 08:18 AM, Eric Snow wrote:
>> Good points. In either case there is no definition order available.
>> I'm okay with that. Would it be better to represent that as None (and
>> the attribute is always defined) or by having the attribute undefined?
>> I'd rather always have the attribute, but I expect the significantly
>> simpler solution is to leave the attribute undefined when definition
>> order is not available. So I'd go with the latter.
> So in Python space the options are either:
> if some_class.__definition_order__ is not None:
> if getattr(some_class, '__definition_order__', None) is not None:
> Seems like always defined would be easier.
Certainly it's easier to use if you need the definition order.
However, I expect it wouldn't be used often enough that that would
make a big difference. Furthermore, if it's always there then it will
always show up when you dir(SomeClass) or look at SomeClass.__dict__.
People would see that on every class, whether it actually provided
definition order or not. I'm not sure that is as helpful as only
having the attribute around when order is defined.
This got me thinking. The attribute should be a class-only attribute
(i.e. a property on type) like __name__ is. That would keep it from
showing up on instance lookups and on every vars(obj).
The big thing for me, though, is that optionally having the attribute
accommodates Python implementations that can't (or don't) preserve
definition order. They don't have to worry about adding a dummy
attribute to every class just for the sake of complying with the spec.
It also means the implementation for dynamic types like Nick
mentioned don't necesarily need to be touched. The more I think about
it the more the optionally set attribute approach is the way to go.
More information about the Python-Dev