Private variables

Delaney, Timothy tdelaney at
Fri Oct 18 05:41:08 CEST 2002

> From: Timothy J. Wood [mailto:tjw at]
>    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.

If your users must truly be considered as adversaries (i.e. you *cannot*
trust them) then Python is not the correct language to use.

However, in most cases you don't need to do this. Simply protecting people
from making mistakes is usually enough - you rarely need to protect against

The simple fact is, if someone has the source to your code, or has the
bytecode, then it doesn't matter what you do to prevent maliciousness - it
becomes a trivial matter to circumvent.

I would suggest you take a long hard look at your requirements and see if
you truly do need this level of security, or if it would simply be enough to
have a system which will prevent mistakes (such as a proxy class which
unconditionally throws an exception from __setattr__).

Tim Delaney

More information about the Python-list mailing list