Named code blockes
Rainer Deyke
root at rainerdeyke.com
Mon Apr 23 22:47:24 EDT 2001
Alex Martelli writes:
>
> > Unnamed blocks are a very powerful programming device. As far as I can
>
> But where's the extra power in making them UNNAMED? It's
> so easy to name them, after all.
Leaving things unnamed can make code a lot more readable (and writable).
Consider:
def f(a, b, c):
return (-b + (b*b - 4*a*c) ** 0.5) / (2 * a)
vs.
def f(a, b, c):
negative_b = -b
b_squared = b*b
four_a = 4 * a
four_a_c = four_a * c
b_squared_minus_four_a_c = b_squared - four_a_c
radical_b_squared_minus_four_a_c = b_squared_minus_four_a_c ** 0.5
numerator = negative_b + radical_b_squared_minus_four_a_c
demoninator = 2 * a
result = numerator / demominator
return result
In Python, unnamed objects have an additional benefit: there is a single
consistent way of naming them. Consider:
a = 5
b = lambda: None
def c():
pass
These are three assignments, but only two look like assingments. The
special syntax for the third case does not make it clear to newbies that 'c'
is a reference like any other which happens to refer to a function object.
--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games - http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor
More information about the Python-list
mailing list