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

M

markturansky

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...
 
M

Mariano Draghi

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:
.... print "bar"
....
>>> m = locals()["foo"]
>>>
>>> m() bar
>>>

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

Hope that helps
 
S

Sybren Stuvel

(e-mail address removed) enlightened us with:
var m = this["MethodName"];
m(); //this invokes a method in javascript

How do I do the same in python?

getattr. Read the documentation, it's all in there.

Sybren
 
L

Leif K-Brooks

I'd like to dynamically find and invoke a method in a Python CGI.

getattr(self, methodName)()

But make sure to validate user input first, of course.
 
G

Gregory Bond

I'd like to dynamically find and invoke a method in a Python CGI.

boundmeth = obj.meth # nb: no ()

# stuff.....

boundmeth() # call it, with args if needed
 
M

markturansky

Yes, that is exactly what I was looking for. Thank you. I will read
more about 'locals()', but a quick test proved you right.
 
M

markturansky

'self' is not defined in this circumstance. The poster above (using
'locals()') has it right.
 
M

markturansky

getattr does not work on a running script (afaik) because 'self' is
undefined. The poster above got it right using 'locals()'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top