By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,675 Members | 2,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,675 IT Pros & Developers. It's quick & easy.

running a function in a different scope

P: n/a

I've got a class that receives a function in the constructor and uses the
__call__ method to execute the aforementioned function when the instance
object is called:

class foo(object):
def __init__(self, func):
self.func = func
def __call__(self, *arg, **kwarg):
return self.func(*arg, **kwarg)

This is ok until I reassign i.e. the builtin function "vars":
vars = foo(vars)

{'self': < object at 0x401ee78c>, 'kwarg': {}, 'arg': ()}

instead of the scope:
{'__builtins__': <module '__builtin__' (built-in)>, '__file__':
'/home/merkosh/.pythonrc', 'sys': <module 'sys' (built-in)>, '__name__':
'__main__', 'foo': <class ''>, '__doc__': None}

Now the function "vars" does not bother me that much. I wonder which other
side-effects will occur, which functions may not be called from within a
foo-object and perhaps how to avoid this, i.e. can the foo-instance detect
in which scope it was executed and call the passed function (vars) in that
particular scope?

Thanks for any hints.
Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.