Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Calling a method with a variable name

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


simon at mullis

Nov 4, 2009, 6:54 AM

Post #1 of 4 (51 views)
Permalink
Calling a method with a variable name

Hi All,

I'm collating a bunch of my utility scripts into one, creating a
single script to which I will symbolic link multiple times. This way
I only have to write code for error checking, output-formatting etc a
single time.

So, I have

~/bin/foo -> ~/Code/python/mother_of_all_utility_scripts.py
~/bin/bar -> ~/Code/python/mother_of_all_utility_scripts.py
~/bin/baz -> ~/Code/python/mother_of_all_utility_scripts.py

I would like "bar" to run the bar method (and so on).

-------------
class Statistic()
def __init__(self):
pass

def foo(self):
return "foo!"

def bar(self):
return "bar!"

# ... and so on...

def main():
stats_obj = Statistic()
name = re.sub("[^A-Za-z]", "", sys.argv[0])
method = getattr(stats_obj, name, None)
if callable(method):
stats_obj.name() # <------------HERE
else:
print "nope, not sure what you're after...."
-----------

However, as I'm sure you've all noticed already, there is no method
called "name". I would really prefer to get a nudge in the right
direction before I start evaling variables and so on.

Does my approach make sense? If not, please give me a hint...

Thanks

SM
--
http://mail.python.org/mailman/listinfo/python-list


deets at nospam

Nov 4, 2009, 6:57 AM

Post #2 of 4 (47 views)
Permalink
Re: Calling a method with a variable name [In reply to]

Simon Mullis wrote:

> Hi All,
>
> I'm collating a bunch of my utility scripts into one, creating a
> single script to which I will symbolic link multiple times. This way
> I only have to write code for error checking, output-formatting etc a
> single time.
>
> So, I have
>
> ~/bin/foo -> ~/Code/python/mother_of_all_utility_scripts.py
> ~/bin/bar -> ~/Code/python/mother_of_all_utility_scripts.py
> ~/bin/baz -> ~/Code/python/mother_of_all_utility_scripts.py
>
> I would like "bar" to run the bar method (and so on).
>
> -------------
> class Statistic()
> def __init__(self):
> pass
>
> def foo(self):
> return "foo!"
>
> def bar(self):
> return "bar!"
>
> # ... and so on...
>
> def main():
> stats_obj = Statistic()
> name = re.sub("[^A-Za-z]", "", sys.argv[0])
> method = getattr(stats_obj, name, None)
> if callable(method):
> stats_obj.name() # <------------HERE
> else:
> print "nope, not sure what you're after...."
> -----------
>
> However, as I'm sure you've all noticed already, there is no method
> called "name". I would really prefer to get a nudge in the right
> direction before I start evaling variables and so on.
>
> Does my approach make sense? If not, please give me a hint...

You are almost there. Why don't you do

if callable(method):
method()

?

Diez
--
http://mail.python.org/mailman/listinfo/python-list


carsten.haese at gmail

Nov 4, 2009, 7:04 AM

Post #3 of 4 (47 views)
Permalink
Re: Calling a method with a variable name [In reply to]

Simon Mullis wrote:
> def main():
> stats_obj = Statistic()
> name = re.sub("[^A-Za-z]", "", sys.argv[0])
> method = getattr(stats_obj, name, None)
> if callable(method):
> stats_obj.name() # <------------HERE
> else:
> print "nope, not sure what you're after...."
> -----------
>
> However, as I'm sure you've all noticed already, there is no method
> called "name". I would really prefer to get a nudge in the right
> direction before I start evaling variables and so on.

At the point you marked "HERE", you've already found the method, and you
have determined that it is callable. You just need to call it. Like
this: method().

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net

--
http://mail.python.org/mailman/listinfo/python-list


simon at mullis

Nov 4, 2009, 7:20 AM

Post #4 of 4 (47 views)
Permalink
Re: Calling a method with a variable name [In reply to]

May I be the first to say "Doh!"

Problem solved, many thanks to both Carsten and Diez!

SM

2009/11/4 Carsten Haese <carsten.haese[at]gmail.com>:
> Simon Mullis wrote:
>> def main():
>>     stats_obj = Statistic()
>>     name = re.sub("[^A-Za-z]", "", sys.argv[0])
>>     method = getattr(stats_obj, name, None)
>>     if callable(method):
>>         stats_obj.name()              #  <------------HERE
>>     else:
>>         print "nope, not sure what you're after...."
>> -----------
>>
>> However, as I'm sure you've all noticed already, there is no method
>> called "name". I would really prefer to get a nudge in the right
>> direction before I start evaling variables and so on.
>
> At the point you marked "HERE", you've already found the method, and you
> have determined that it is callable. You just need to call it. Like
> this: method().
>
> HTH,
>
> --
> Carsten Haese
> http://informixdb.sourceforge.net
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
Simon Mullis
_________________
simon[at]mullis.co.uk
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.