Teaching Programming

superpollo utente at esempio.net
Tue May 4 07:56:43 EDT 2010


Stefan Behnel ha scritto:
> superpollo, 04.05.2010 13:23:
>> Stefan Behnel ha scritto:
>>> the main reason why this problem doesn't hurt much in Python
>>> is that Python is a dynamic language that can get you extremely far
>>> without generating code. It's simply not necessary in most cases, so
>>> people don't run into problems with it.
>>
>> Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45)
>> [GCC 4.3.3] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>  >>> A,B=2,3
>>  >>> if A>B:
>> ... print A+B
>> ... else:
>> ... print A**B-B**2
>> ...
>> -1
>>  >>> A,B=3,2
>>  >>> if A>B:
>> ... print A+B
>> ... else:
>> ... print A**B-B**2
>> ...
>> 5
>>
>> tell me please: how can generate the same output (depending on A and B)
>> without control structure? i mean in a natural "pythonic" way...
> 
> The question is: why do you have to generate the above code in the first 
> place? Isn't a function enough that does the above?

of course! *but* if i must generate on-the-fly python code that defines 
a function i am back again to the problem:

def fun():
     ....

ecc...

how can i put *that* on a oneliner?



More information about the Python-list mailing list