empty classes as c structs?
Alex Martelli
aleaxit at yahoo.com
Sun Feb 6 17:26:11 EST 2005
Steven Bethard <steven.bethard at gmail.com> wrote:
> Seems pretty reasonable -- the only thing I worry about is that
> classmethods and other attributes (e.g. properties) that are accessible
> from instances can lead to subtle bugs when a user accidentally
> initializes a Bunch object with the attributes of the same name, e.g.:
>
> b = Bunch(getDict=1)
>
> where
>
> b.getDict()
>
> now fails with something like "TypeError: 'int' object is not callable".
Well, that's the problem with confusing items and attributes in the
first place, of course -- which IS Bunch's purpose;-)
> (For another discussion about this problem, see [1]).
>
> I don't know what the right solution is here... I wonder if I should
> write a classmethod-style descriptor that disallows the calling of a
> function from an instance? Or maybe I should just document that the
> classmethods should only be called from the class? Hmm...
Another approach is to add a few "reserved words" to the ones Python
itself would reject in the initialization. Just you cannot do:
b = Bunch(continue=23)
you may choose to forbid using getDict=42 - if you do that you probably
want to forbid any magicname too, since e.g.
b = Bunch(__dict__=99)
can't work ``right'' no matter what, while setting e.g. __deepcopy__
similarly might confuse any copy.deepcopy(b), etc, etc.
> How do you feel about getDict and setDict also being classmethods?
Uh? I want to get or set the dict of a specific instance -- not those
of the whole class. How would you design them as classmethods...?
Alex
More information about the Python-list
mailing list