indirect function calls and variable variables 
Author Message
 indirect function calls and variable variables



Quote:

>> Question:  Is it feasible to make indirect function calls in python?
>    ...
>> #what i'm trying to avoid
>> def do_f(f):
>>         if f == 'f1': f1()
>>         if f == 'f2': f2()
>>         if f == 'f3': f3()
>>         if f == 'fInfinity': youGetThePoint()

>> #what i'd like to do instead
>> def do_f(f):
>>         f() # this doesn't work, but maybe you get the point.

>Sure, try:

>    dispatcher = {'f1': f1, 'f2': f2, 'f3': f3, ...}
>    def do_f(f):
>        dispatcher[f]()

>> While I'm at it, can python do variable variables?
>> In PHP:
>>         $var = 'hey'
>>         $$var = 'dude'

>>         echo $dude

>>         output: hey

>> I used this alot in PHP.  Is there a way to do it in Python?

>You can do it using exec, but the best solution is a separate
>dictionary.

                        .
                        .
                        .
I'll provide more detail.

PHPers do indeed use variable variables often--too much,
in fact.  It's a general principle that languages which
facilitate variable variables also are likely to offer
a dictionary or associative array, and most uses of
variable variables would better be recoded in terms of
dictionaries.  There are PHP folk who know this already.
If there's a question about it, someone invite me to
compl.lang.php, and I'll elaborate.

Erik illustrated the pattern with his function invocation
above.  He didn't write there that such usage generally
invites consideration for refactoring into an object-
oriented polymorphism scheme.  I leave that as an exercise
for the reader.  He also didn't observe that, yes, do_f is
feasible in a more narrow sense, using either of at least
a couple of distinct approaches.  Here's an illustration:

  def do_f(f):
          # You can do much the same with exec, instead.
      eval("%s()" % f)

  def do_f_other(f):
          # Other evaluations involve call() or __getattr__().
      (globals()[f])()

  def f1():
      print "I am f1."

  def f2():
      print "I am f2."

  do_f("f1")
  do_f("f2")
  do_f_other("f1")

Finally, can Python process variable variables?  Sure, but,
again, I'll warn you against this; there are usually better
ways:
  var = 'hey'
  dude = 'var'
  print eval(dude)

Also, your original PHP example doesn't do what you say it
does.  I leave this, too, as an exercise for the reader.
--


Business:   http://www.*-*-*.com/
Personal:   http://www.*-*-*.com/



Sun, 06 Nov 2005 20:41:43 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. indirect function calls and variable variables

2. Class variables / global variables / Init variables

3. calling function recursively, local variables?

4. Local variables, call-back functions under Windows

5. evaluating a variable as a function call?

6. Function call or variable?

7. Function call or variable?

8. calling a variable from a separate function

9. Call a function using a variable

10. Indirect function member calls

11. Question regarding indirect function calls

12. How update variable values by calling secondary functions ?

 

 
Powered by phpBB® Forum Software