empty classes as c structs?
Steven Bethard
steven.bethard at gmail.com
Mon Feb 7 00:31:07 EST 2005
Michael Spencer wrote:
> ISTM that 'bunch' or 'namespace' is in effect the complement of vars
> i.e., while vars(object) => object.__dict__, namespace(somedict) gives
> an object whose __dict__ is somedict.
Yeah, I kinda liked this application too, and I think the symmetry would
be nice.
> Looked at this way, namespace (or bunch) is a minimal implementation of
> an object that implements the hasattr(object,__dict__) protocol. The
> effect of the class is to make operations on __dict__ simpler.
> namespace instances can be compared with any other object that has a
> __dict__. This differs from the PEP reference implementation which
> compares only with other bunch instances.
Yeah, I wanted to support this, but I couldn't decide how to arbitrate
things in update -- if a dict has a __dict__ attribute, do I update the
Namespace object with the dict or the __dict__? That is, what should I
do in the following case:
py> class xdict(dict):
... pass
...
py> d = xdict(a=1, b=2)
py> d.x = 1
py> d
{'a': 1, 'b': 2}
py> d.__dict__
{'x': 1}
py> Namespace(d)
The dict d has both the items of a dict and the attributes of a
__dict__. Which one gets assigned to the __dict__ of the Namespace? Do
I do:
self.__dict__ = d
or do I do:
self.__dict__ = d.__dict__
It was because these seem like two separate cases that I wanted two
different functions for them (__init__ and, say, dictview)...
Steve
More information about the Python-list
mailing list