473,322 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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

Jul 21 '05 #1
7 1426
ma**********@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

Jul 21 '05 #2
ma**********@gmail.com wrote:
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.
Jul 21 '05 #3
ma**********@gmail.com 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
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Jul 21 '05 #4
ma**********@gmail.com wrote:
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
Jul 21 '05 #5
Yes, that is exactly what I was looking for. Thank you. I will read
more about 'locals()', but a quick test proved you right.

Jul 21 '05 #6
'self' is not defined in this circumstance. The poster above (using
'locals()') has it right.

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

Jul 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
5
by: Jesee | last post by:
I am reading Jeffrey Richter's book "Applied Microsoft .NET Framework programming",i came across "Exception handing". Page 405 says "If the stack overflow occurs within the CLR itself,your...
4
by: p988 | last post by:
using System; using System.Windows.Forms; using System.Drawing; class MyForm : Form { MyForm () { Text = "Windows Forms Demo"; }
175
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
8
by: kevin | last post by:
I have a form and in the form I have a sub that uses a class I instantiate using visual basic code: Public oCP As New Rs232 'instantiate the comm port I need to share this sub with...
0
by: Kaimar Seljamäe | last post by:
Hi, I have to create a web service client which uses SOAP encoding but does not use "multi-reference" values (see http://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383513 item 10). If I...
6
by: Marty | last post by:
Hi, I have a class that I modified to be static. It is now a public sealed class and all function are static, no more constructor but a init() function to do the constructor job. This class...
12
by: Robert Fuchs | last post by:
Hello, This example: public class BaseC { public int x; public void Invoke() {} } public class DerivedC : BaseC
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.