Private variables

Timothy J. Wood tjw at omnigroup.com
Thu Oct 17 22:12:55 EDT 2002


   Is there any way to have truly private instance variables in Python?

   My situation is this -- I'm going to be working on a module that has 
C 'native methods'.  Several of the Python classes will correspond to C 
structures that will be used in these native methods.  Additionally, 
I'll want to allow (along with other security measures) to be able to 
safely run third-party code that uses my module.

   Now, the Python instance needs to be able to hold onto its C instance 
somehow.  I don't particularly care if other Python scripts can read 
the opaque pointer, but they shouldn't be able to do anything with it 
(like, set it to some other value).

   The PyCObject doesn't seem to help since, while it wraps a C pointer, 
my regular Python object would still have to hold onto it and a 
malicious user could, say, swap a PyCObject for class A with one for 
class B, resulting in a crash at best.

   The support for adding built-in types doesn't seem to help since it 
doesn't look like you can subclass from them (or can you?)

   Any pointers to the 'right' way to do this would be very helpful.

   Thanks!

-tim





More information about the Python-list mailing list