AW: Are there any PEPs for typed arguments?

F. GEIGER fgeiger at datec.at
Mon Dec 3 03:06:15 EST 2001


> I think that a better question is why are you adding these asserts in
> the first place?

Call it a strange habit, but I like to catch bugs as early as possible.
Sure, some way down the code such errors will show up. But then you have to
figure out where and how it was introduced. For that you likely will use
assertions...

> Are you claiming that "meth" would not work correctly if I passed a
> small long for "a" or that I could not use a instance that supports
> __getitem__ (that always returns a length 1 string), __len__ and a few
> other string methods for "b"?

Okay, okay. So take this one:

def meth(mine, yours):
   assert isinstance(mine, MyClass)
   assert isinstance(yours, YourClass)
   <body>
   return

meth(YourClass(), MyClass()) # Wrong order of args


Another thought: If I document my code, I often do it like so:

'''
[String] = object.meth(mine: MyClass, yours: YourClass)
'''

This already says it all. I do not need to write "mine is of type MyClass
and you should assure that this is fullfilled" or something like that. I
also do not have to write in a Windows-like manner "myclassMine is of type
you guess by its prefix, it's of type MyClass".

So now I have to implement it. To assure requirements and promises as
documented I have to convert

object.meth(mine: MyClass, yours: YourClass)

into

def object.meth(mine, yours):
   assert isinstance(mine, MyClass)
   assert isinstance(yours, YourClass)

I do this over and over again. An interpreter could do this once, when it
compiles my sources to byte-code. Again: No static type checking, only
convert some notion into assertions. No big deal. Simply a method to catch
bugs already in a first shot, even before writing and executing test beds.


Cheers
Franz


P.S.: It was not my intention to start a new "type-system thread". OTOH I
did not know that there's already that much stuff discussed on topics like
this. There's even a SIG existing for this (as an other poster pointed out)!


> -----Ursprungliche Nachricht-----
> Von: Brian Quinlan [mailto:brian at sweetapp.com]
> Gesendet: Sonntag, 02. Dezember 2001 20:08
> An: 'F. GEIGER'
> Cc: python-list at python.org
> Betreff: RE: Are there any PEPs for typed arguments?
>
>
> F. GEIGER wrote:
> > Are there any ideas to promote these lines
> >
> > def meth(a, b, c, d):
> >    assert type(a) == IntType
> >    assert type(b) == StringType
> >    assert isinstance(c, MyClass)
> >     <meth body>
> >    return
> >
> > into something like this:
> >
> > def meth(a: IntType, b: StringType, c: MyClass, d):
> >    <meth body>
> >    return
> >
> > in a future version of Python?
>
> I think that a better question is why are you adding these asserts in
> the first place?
>
> Are you claiming that "meth" would not work correctly if I passed a
> small long for "a" or that I could not use a instance that supports
> __getitem__ (that always returns a length 1 string), __len__ and a few
> other string methods for "b"?
>
> So why restrict the use of compatible types?
>
> Cheers,
> Brian
>
>





More information about the Python-list mailing list