changing local namespace of a function

Jeff Shannon jeff at ccvcorp.com
Fri Feb 4 18:54:18 EST 2005


Bo Peng wrote:

> My function and dictionaries are a lot more complicated than these so I 
> would like to set dict as the default namespace of fun. 

This sounds to me like you're trying to re-implement object orientation.

Turn all of those functions into methods on a class, and instead of 
creating dictionaries that'll be passed into functions, create class 
instances.

class MyClass(object):
     def __init__(self, **kwargs):
         for key, val in kwargs:
             setattr(self, key, val)
     def fun(self):
         self.z = self.y + self.x

a = MyClass(x=1, y=2)
a.fun()
print a.z

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list