473,546 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1431
ma**********@gm ail.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**********@gm ail.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**********@gm ail.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**********@gm ail.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
2330
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 Methods Version: $Revision: 1.34 $ Last-Modified: $Date: 2004/09/03 09:32:50 $ Author: Kevin D. Smith, Jim Jewett, Skip Montanaro, Anthony Baxter
5
17246
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 application code won't be able to catch the StackOverflowException exception and none of your finally blocks will excute.",I don't understand it. Following...
4
7331
by: p988 | last post by:
using System; using System.Windows.Forms; using System.Drawing; class MyForm : Form { MyForm () { Text = "Windows Forms Demo"; }
175
8716
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 changed". This is very much against my instincts. Can anyone offer some solid design guidelines for me? Thanks in advance....
9
2358
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 class, if the class has more than than one cosntructor, by making sure that each constructor has a different signature. I have managed to learn that...
8
11425
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 another form so to declare the sub I use visual basic code: Public Shared Function IsPortAvailable(ByVal ComPort As Integer) As Boolean
0
1929
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 create SOAP client like this: public class StockService : SoapHttpClientProtocol {
6
2701
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 inherit from a QuickFix.Application class. I have this object: private static SocketInitiator qfxInitiator; When instantiated, its constructor...
12
4787
by: Robert Fuchs | last post by:
Hello, This example: public class BaseC { public int x; public void Invoke() {} } public class DerivedC : BaseC
0
7504
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7435
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7792
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6026
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3491
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.