Polymorphing - how?

Russo, Tom tom at studentuniverse.com
Wed May 1 16:53:51 EDT 2002


> I would like to execute functions which names will be changed during
> runtime. The following approach does not work. :-(
> 
> def First(s):
> 	print "myFun1: %s" % s
> def Second(s)
> 	print "myFun2: %s" % s
> name = 'First'
^^^^^^^^^^^^^^^^^^
this line is the problem

> name('blah') # It doesn't work. :-(
> 			 # TypeError: 'str' object is not callable
> name = 'Second'
^^^^^^^^^^^^^^^^^^^^^^
along with this one.

You want to say:
name = First
name = Second

_not_ 'First' or 'Second'
The way you're doing it now just puts a string into the variable name.
Assigning without the single quotes around First and Second puts the
relevant function object into name.

_t


> name('blah') # Like above. 
> 
> The following code in PHP does work:
> 
> <?php
> function First($s) { print "myFun1: $s"; }
> function Second($s) { print "myFun2: $s"; }
> 
> $name = 'First';
> $name('blah'); # It works.
> print "\n";
> $name = 'Second'; # It works.
> $name('blah');
> ?>
> 
> Is it possible for Python? 
> 
> --
> Jarosław Zabiełło (UIN: 6712522)
> URL: http://3585753410/~zbiru
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 





More information about the Python-list mailing list