|
|
|
generator = (f(value) for value in iterable if value > 0)
That syntax is just syntactic sugar for an older way of doing Python generator functions, which just requires defining a function that uses the "yield" keyword instead of "return". In other words, it's equivalent to:
def generator():
for value in interable:
if value > 0:
yield f(value)
Of course, the former syntax is more concise in this case, but the extended syntax allows for more complicated constructions.
L33tminion |
Homepage |
08/09/04 - 5:25 pm | #
|
|
Ack, I put that on the wrong post. My apologies.
L33tminion |
Homepage |
08/09/04 - 5:27 pm | #
|
|
"It is fair to say that out parameters are only needed in C# because it can't return multiple values"
Welllll, *technically* Python methods can't return multiple values, either. It just makes it really easy to return tuples.
As a Lisp programmer, it is fair to say that Python makes returning tuples so easy because it can't actually return multiple values. 
For example, if I write your product-and-difference example in Lisp, and call it from a function that only uses the first value, my Lisp compiler won't even generate code to pull out the second value -- on the caller side, it generates identical machine code as if calling a function that only returned one value.
The Python compiler has no concept of multiple return values, so it can't do this. Internally, it's just returning a single tuple.
Nitpicker |
09/09/09 - 6:36 pm | #
|
|
|
Commenting by HaloScan
|