osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

type hinting backward compatibility with python 3.0 to 3.4


On Sat, 20 May 2017 11:57 am, Chris Angelico wrote:

> They're function metadata. What would the principle of least surprise
> say about this?
> 
> print("Spam")
> def func(arg: print("Foo") = print("Quux")):
>     print("Blargh")
> print("Fred")
> func()
> print("Eggs")
> 
> What should be printed, and in what order?

My prediction:

Spam
Foo
Quux
Fred
Blargh
Eggs

but I wouldn't be the least bit astonished if Foo and Quux are in the
opposite order. As in fact they are.

> Actually, Python does violate least-surprise in one area here. There's
> one message that gets printed "out of order" compared to my
> expectation. I wonder if it's the same one that other people will be
> surprised at.


The reason appears to be that the default arguments are evaluated first,
from left to right, followed by the annotations:


py> def func(a:print(1)=print(2), b:print(3)=print(4), c:print(4)=print(5)):
...     pass
...
2
4
5
1
3
4




-- 
Steve
Emoji: a small, fuzzy, indistinct picture used to replace a clear and
perfectly comprehensible word.