is there an equivalent of javascript's this["myMethod"] for the currently running script?

Mariano Draghi chaghi at sion.com
Tue Jul 5 23:48:56 EDT 2005


markturansky at gmail.com wrote:
> I'd like to dynamically find and invoke a method in a Python CGI.
> 
> In javascript, the running script is 'this' (Python's 'self'), except
> that 'self' is not defined.
> 
> I want to do this:
> 
> var m = this["MethodName"];  //where the method name is passed via an
> http variable
> m();  //this invokes a method in javascript
> 
> How do I do the same in python?
> 
> self["MethodName"] fails...
> 

Don't know if this is the best solution... but you need something around 
the lines of:

 >>> def foo():
...     print "bar"
...
 >>> m = locals()["foo"]
 >>>
 >>> m()
bar
 >>>

i.e., you need to play a bit with locals()

Hope that helps

-- 
Mariano




More information about the Python-list mailing list