>
>  private_hash = dict( A=42, B=69 )
>  def public_fn(param):
>    return private_hash[param]
>  print public_fn("A")     # good:  prints 42
>  x = private_hash["A"]    # works: oops, hash is in scope
>
> I'm not happy with that because I'd like to limit the scope of the
> private_hash variable so that it is known only inside public_fn.
>

A common idiom to do that would be this:

def public_fn(param, __private_hash=dict(A=42, B=69)):
    return __private_hash[param]

People often squint when they see that but it is useful in several ways.
Python initializes the default arguments -once-, at definition. If they are
mutable-- a dictionary, list or such-- that same dict is used everytime the
function is called.

HTH,

--S
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to