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

Discussion in 'Python' started by markturansky@gmail.com, Jul 6, 2005.

  1. Guest

    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 6, 2005
    #1
    1. Advertising

  2. Re: is there an equivalent of javascript's this["myMethod"] for thecurrently running script?

    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
     
    Mariano Draghi, Jul 6, 2005
    #2
    1. Advertising

  3. 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
     
    Sybren Stuvel, Jul 6, 2005
    #3
  4. Re: is there an equivalent of javascript's this["myMethod"] forthe currently running script?

    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.
     
    Leif K-Brooks, Jul 6, 2005
    #4
  5. Gregory Bond Guest

    Re: is there an equivalent of javascript's this["myMethod"] for thecurrently running script?

    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
     
    Gregory Bond, Jul 6, 2005
    #5
  6. Guest

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

    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 7, 2005
    #6
  7. Guest

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

    'self' is not defined in this circumstance. The poster above (using
    'locals()') has it right.
     
    , Jul 7, 2005
    #7
  8. Guest

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

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

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Blatt
    Replies:
    0
    Views:
    379
    Peter Blatt
    Oct 9, 2004
  2. shraddha
    Replies:
    10
    Views:
    6,115
    shraddha
    May 31, 2005
  3. Brian
    Replies:
    2
    Views:
    2,824
    Brian
    Dec 27, 2004
  4. Yeounkun, Oh

    How can I get a name of function running currently.

    Yeounkun, Oh, Nov 16, 2004, in forum: C Programming
    Replies:
    2
    Views:
    444
    Stuart Gerchick
    Nov 16, 2004
  5. Sean Farrow
    Replies:
    1
    Views:
    293
Loading...

Share This Page