Python Sanity Proposal: Type Hinting Solution

Roy Smith roy at panix.com
Sat Jan 24 09:06:57 EST 2015


In article <54c3a0c1$0$13013$c3e8da3$5496439d at news.astraweb.com>,
 Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:

> 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.

On the other hand, flip it around and say:

def sqrt(int as i):

and now the name-binding semantics works.  "The first argument is an 
int, which is bound to the name i".  Not sure how you would apply that 
to the return type, though.



More information about the Python-list mailing list