Python Sanity Proposal: Type Hinting Solution

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Jan 24 08:40:15 EST 2015


Tim Chase wrote:

> On 2015-01-24 17:21, Steven D'Aprano wrote:
>> # Cobra
>> def sqroot(i as int) as float
>> 
>> # Python
>> def sqroot(i:int)->float:
>> 
>> 
>> Cobra's use of "as" clashes with Python. In Python, "as" is used for
>> name-binding:
>> 
>> import module as name
>> with open('file') as f
>> except Exception as e
>> 
>> but apart from that minor difference, they're virtually identical.
> 
> Though given that
> 
>  def sqrt(i as int) as float:
> 
> is invalid Python syntax (both in the parameter list and as the
> return value of the function), the meaning of "as" could be overloaded
> in both senses to allow for the Cobra-like syntax.

OF course it could. But it shouldn't, because "as" already has an
established and consistent meaning: it is used for name binding.

Things which look similar should be similar. "i as int" and "module as name"
look similar, but they are the same syntax for different concepts. "foo as
bar" will sometimes mean that bar is the name bound to foo, and sometimes
it will mean that foo is declared to be type bar. That's messy and
inconsistent and best avoided.



-- 
Steven




More information about the Python-list mailing list