[C++-sig] Disallow adding attributes to wrapped classes from Python
Andreas Beyer
beyer at imb-jena.de
Mon Dec 13 10:56:49 CET 2004
I think it should be possible to catch the __setattr__() of __dict__ and
to throw an exception. However, I think that misses out the philosophy
of Python. Variables are not declared. It is up to the user to define
appropriate test cases that check the correctness of the code.
You could also define a function that sets the value of foobar. If the
user writes
foo.set_foobaz(7)
he/she will get an exception.
Andreas
Stefan Haller wrote:
> Is there a way to make the __dict__ of exposed classes read-only? I
> fear it will be a common error for Python programmers to make typos when
> setting attributes, and I would like this to generate an error rather
> than silently adding a new attribute.
>
> class_<Foo>("Foo")
> .add_property("foobar", &Foo::getFooBar, &Foo::setFooBar)
> ;
>
>
>>>>foo.foobaz = 7 # oops: typo!
>>>>foo.foobaz
>
> 7
>
>
--
--------------------------------------
Dr. Andreas Beyer - beyer at imb-jena.de
http://www.imb-jena.de/~beyer
--------------------------------------
More information about the Cplusplus-sig
mailing list