generator functions: why won't this work?

Mel mwilson at the-wire.com
Wed Apr 2 10:57:50 EDT 2008


zillow10 at googlemail.com wrote:
I'd just like to test my
> understanding of this. Suppose I create the following generator
> object:
> 
> g = getNextScalar(1, 2, (3, 4), 5)
> 
> when the iterator reaches the tuple argument (3, 4) then, according to
> Steve and George, the * in *arg causes this tuple to be expanded into
> positional arguments, and it makes sense to do it this way. But what
> happens when getNextScalar(arg) is used instead? 

Try it:

Python 2.5.1 (r251:54863, Mar  7 2008, 04:10:12)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> def a (arg):
...   print arg
...
 >>> def astar (*arg):
...   print arg
...
 >>> a(3,4)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: a() takes exactly 1 argument (2 given)
 >>> astar(3,4)
(3, 4)
 >>> a((3,4))
(3, 4)
 >>> astar((3,4))
((3, 4),)
 >>>




	Mel.




More information about the Python-list mailing list