Private variables

Rasjid Wilcox rasjidw at
Thu Sep 2 03:10:04 CEST 2010

Hi all,

I am aware the private variables are generally done via convention
(leading underscore), but I came across a technique in Douglas
Crockford's book "Javascript: The Good Parts" for creating private
variables in Javascript, and I'd thought I'd see how it translated to
Python. Here is my attempt.

def get_config(_cache=[]):
    private = {}
    private['a'] = 1
    private['b'] = 2
    if not _cache:
        class Config(object):
            def a(self):
                return private['a']
            def b(self):
                return private['b']
        config = Config()
        config = _cache[0]
    return config

>>> c = get_config()
>>> c.a
>>> c.b
>>> c.a = 10
Traceback (most recent call last):
  File "<string>", line 1, in <fragment>
AttributeError: can't set attribute
>>> dir(c)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'b']
>>> d = get_config()
>>> d is c

I'm not really asking 'is it a good idea' but just 'does this work'?
It seems to work to me, and is certainly 'good enough' in the sense
that it should be impossible to accidentally change the variables of

But is it possible to change the value of c.a or c.b with standard
python, without resorting to ctypes level manipulation?



More information about the Python-list mailing list