[C++-sig] disallow instance attributes created by assignement to nonexistent class attribute

Václav Šmilauer eudoxos at arcig.cz
Thu Apr 8 11:36:00 CEST 2010


Hi all,

I have classes wrapped with boost::python defining certain attributes.
If user assigns value to an attribute that doesn't exist as class
attribute, python automatically creates instance attribute of that name.

I would like to avoid this (although I know that is the way Python is
supposed to work), as python classes, in my case, model c++ classes
closely: AttributeError should be raised.

It happened to me (and other people) that I assigned nonexistent
attribute (by typo or by confusing the name in my head), then debugging
the c++ code to see why it did not have the effect it should obviously
have.

I wasn't able to find answer via google, besides overriding __setattr__,
which would have most likely have performance implications. I appreciate
any pointers or suggestions.

Thanks,

Václav



More information about the Cplusplus-sig mailing list