seeking deeper (language theory) reason behind Python design choice

Stefan Klinger mail at stefan-klinger.de
Mon May 7 18:52:01 EDT 2018


Chris Angelico (2018-May-08, excerpt):
> What exactly would be the scope of the assigned name?

Yes, that's more like the kind of answer I was seeking. But I'm not
entirely satisfied.
> def sort_func(item):
> date = parse_date(item.creation_date)
> return date.day_of_week, date.year, date.month, date.day
> items.sort(key=sort_func)

This function contains two statements. Would be nice to see a
one-statement variant that exposes the same necessity of local
assignment.
I have not thought about local assignment expressions, but they can
already now be done naturally as follows:
 lambda item: (lambda date: date.day_of_week, date.year, date.month, date.day)(parse_date(item.creation_date)
But this is stretching the usefulness of Python's lambda (readability)
quite a bit, I have to admit.
Anyway, I was rather thinking about global assignment. And there
would even be choice in whether it should be always global, Python has
a rule for that in the following case:
 x = 23
 def foo(y):
 x = 2 * y # this is local
 print('x in foo', x)
 print('x before foo', x)
 foo(42)
 print('x after foo', x)
 class z:
 v = 12345
 def bar(y):
 z.v = 2 * y # this is global
 print('z.v in bar', z.v)
 print('z.v before bar', z.v)
 bar(0)
 print('z.v after bar', z.v)
Cheers
Stefan
--
http://stefan-klinger.de o/X
Send plain text messages only, not exceeding 32kB. /\/
 \


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /