empty classes as c structs?

Alex Martelli aleaxit at yahoo.com
Thu Feb 10 03:29:41 EST 2005


Steven Bethard <steven.bethard at gmail.com> wrote:

> I'm not sure how much _I_ like them... =) It makes me uneasy that
> 
>      del b.x
>      print b.x
> 
> doesn't throw an AttributeError.  OTOH, if you're using namespaces as
> the equivalent of nested scopes, deleting all 'x' attributes is probably
> not what you want...

Right.  Besides, you can easily get such effects today:

>>> b.x = 15
>>> print b.x
15
>>> del b.x
>>> print b.x
23
>>> del b.x
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: x


All you need is to have (e.g.) the following few lines before these:

>>> class B(object):
...   x = 23
... 
>>> b=B()


> I like the idea of chain, though, so I'll probably add the class with
> just __init__ and __getattribute__ to the current implementation.  I'm
> willing to be persuaded, of course, but for the moment, since I can see
> a few different options, I'm refusing the temptation to guess on the 
> "most natural" behavior for __delattr__ and __setattr__...  =)

That's probably best in terms of API.  Not too sure about the
implementation (why wouldn't __getattr__ suffice, holding the bunches in
an attribute with a magicname?) but that's a secondary issue.


Alex



More information about the Python-list mailing list