calling a method using a variable name? 
Author Message
 calling a method using a variable name?

Can I call a method of a class using a variable, eg:

class X:
   def method():
      pass

z=X."method"()

How would I do this?  I need to call different methods (read from a
list) without hardcoding them in the program.

Thanks in advance,
  -Patrick Price



Wed, 31 Aug 2005 04:49:59 GMT  
 calling a method using a variable name?

Quote:

> How about this:

> myMethod = 'method'
> myCall = 'z=X.'+myMethod+'()'
> exec(myCall)


>  > Can I call a method of a class using a variable, eg:

>  > class X:
>  >   def method():
>  >      pass

>  > z=X."method"()

>  > How would I do this?  I need to call different methods (read from a
>  > list) without hardcoding them in the program.

Actually, you should use getattr() instead of exec.

def callMethod(obj, methname, *args, **kwargs):
    meth = getattr(obj, methname)
    return meth(*args, **kwargs)

Jeremy



Wed, 31 Aug 2005 05:36:18 GMT  
 calling a method using a variable name?
How about this:

myMethod = 'method'
myCall = 'z=X.'+myMethod+'()'
exec(myCall)

Hope this helps,
Alan McIntyre

 >
 > Can I call a method of a class using a variable, eg:
 >
 > class X:
 >   def method():
 >      pass
 >
 >
 > z=X."method"()
 >
 > How would I do this?  I need to call different methods (read from a
 > list) without hardcoding them in the program.
 >
 > Thanks in advance,
 >  -Patrick Price

 >



Wed, 31 Aug 2005 05:20:12 GMT  
 calling a method using a variable name?

Quote:

> How about this:

> myMethod = 'method'
> myCall = 'z=X.'+myMethod+'()'
> exec(myCall)

Better yet, use getattr:

        getattr(X, myMethod)(...)

--

 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ There is nothing wrong with sobriety in moderation.
\__/ John Ciardi
    Physics reference / http://www.alcyone.com/max/reference/physics/
 A physics reference.



Wed, 31 Aug 2005 06:32:18 GMT  
 calling a method using a variable name?
Ah, thanks Jeremy.  I hadn't thoroughly read up on exec; here's an
excerpt from the python Essential Reference:

"When a string is passed to exec, eval() or execfile(), the parser first
compiles it into bytecode.  Because this process is expensive, it may be
better to precompile the code and reuse the bytecode on subsequent calls
if the code is going to be executed multiple times."

Or just use getattr to get to the method in this case, as you pointed
out. :)  Building the call string and running exec 100k times takes 15.8
seconds on my machine, whereas using getattr and then calling the method
returned takes .34 sec for 100k repetitions.

Thanks,
Alan

Quote:

> Actually, you should use getattr() instead of exec.

> def callMethod(obj, methname, *args, **kwargs):
>     meth = getattr(obj, methname)
>     return meth(*args, **kwargs)



Wed, 31 Aug 2005 06:15:01 GMT  
 calling a method using a variable name?

Quote:

> class X:
>    def method():
>       pass

> z=X."method"()

> How would I do this?

Notice that writing

z = X.method()

wouldn't work, either, since you cannot call an unbound method
without an instance. So you would have to write

x = X()
z = x.method()

If you want to use a string, write

z = getattr(x, "method") ()

HTH,
Martin



Wed, 31 Aug 2005 07:21:52 GMT  
 calling a method using a variable name?

Quote:
> > class X:
> >    def method():
> >       pass

Shouldn't that be:

class X:
   def method(self):
      pass



Wed, 31 Aug 2005 09:03:08 GMT  
 calling a method using a variable name?

Quote:

> Shouldn't that be:

> class X:
>    def method(self):
>       pass

Oops. Certainly.

Martin



Thu, 01 Sep 2005 17:44:37 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Using a variable name for a variable name??

2. Calling a (any) superclass method when already have one by that name

3. what's the calling method name?

4. Calling a function or method by name

5. call method by name?

6. How to call a class method from a string representing a class name

7. Reflection: Calling Methods Dynamically by Name

8. How to call method by name ? (newbie)

9. Calling superclass method with variable args?

10. Calling a procedure with a variable procedure name

11. call procedure named in a variable?

12. how to call a functino through a variable name

 

 
Powered by phpBB® Forum Software