setattr and variable names: bug or feature?

Moshe Zadka moshez at math.huji.ac.il
Fri Feb 25 11:55:08 EST 2000


On Fri, 25 Feb 2000, Gerrit Holl wrote:

> Dear bottomless information source,
> 
> >>> class Foo:
> ...     pass
> ...
> >>> setattr(Foo, '', '')
> >>> setattr(Foo, '`', '')
> >>> setattr(Foo, '\'', '')
> >>> setattr(Foo, '!@#{}\0\0\0', '')
> >>> dir(Foo)
> ['', '!@#{}\000\000\000', "'", '__doc__', '__module__', '`']
> 
> Is this a bug or a feature, or None of both?

>From the library reference:
   setattr (object, name, value)
          This is the counterpart of getattr(). The arguments are an
          object, a string and an arbitrary value. The string may name an
          existing attribute or a new attribute. The function assigns the
          value to the attribute, provided the object allows it. For
          example, setattr(x, 'foobar', 123) is equivalent to x.foobar =
          123.

So, this is a documented feature: nothing says the string should be 
a Python identifier. One no sane person on earth ever uses, but a 
documented feature nevertheless.

--
Moshe Zadka <mzadka at geocities.com>. 
INTERNET: Learn what you know.
Share what you don't.





More information about the Python-list mailing list