Optional Static Typing

Rahul codedivine at gmail.com
Sat Dec 25 17:26:40 CET 2004


I am assuming that optional type checking is being added for easier
debugging only. So if 'expects' are turned on , python raises
warnings(which do not halt the system) but not when they are turned
off. These will enable easier debugging for new people while not
affecting masters. Also,perhaps, it will be easier to accomodate till
type checking mechanism is perfected(if it is implemented at all that
is) so that python does not stop you when it is in fact  python which
might be making some mistake.(This last statement is a guess only...)

It is similar to assert and __debug__=1 in a way.

So the crux is :
1.Expects is only a bridge between type checking and dynamic typing.
2.Type checking is done only as a tool which you are free to override
if you want to.
3.The objective of type checking here is only to make debugging easier
and not speed/optimization.
4.The point is not that 'expects' be a additional keyword.You can go
like this also :
def (int a,int b): or whatever you like. Only that expects make it a
bit clearer IMHO.

sincerely.,
rahul

Scott David Daniels wrote:
> Rahul wrote:
> > 1.def gcd(a,b) expects (int,int)
>
> I presume by this syntax you mean something like:
>      def gcd(a, b) expects (int, int):
>          if b > a:
>              a, b = b, a
>          while a > b > 0:
>              a, b = b, a % b
>          return a
>
> > Here the function is not enforcing type checking. The compiler
should
> > only generate warning when someone passes something which is not an
int
> > and should not generate an error.Maybe the person calling the
function
> > knows what he is doing and wants to pass the unexpected type.
>
> But if this is the case, why is it different from?:
>      def gcd(a, b): # expects (int, int)
>          return a * b
>
> > 2.Another possibility is to let the function decide if the type is
not
> > what it is expecting. Maybe the function recvs some kind of flag
which
> > tells it that the type passed is not what it was expecting. So it
can
> > throw an error if it is really critical.
>
> Again, why make the test at all if you don't want to act on it?
>   assert <boolexp> aborts if it is checked at all, but with __debug__
> defined as 1, it passes.  Perhaps you are proposing that expects be
> used in that context.
>
> > 3.In my post i am not stressing on adding 'expects' as keyword or
> > something. Only that type should not be enforced and 'expects'
makes
> > this clear.
> You need to explain why anyone would want to write expects at all.
> If you are expecting the code generation to change, you'd better
> enforce, rather than advise, unless you are defining points at which
> to do code specialization on types.
> 
> --Scott David Daniels
> Scott.Daniels at Acm.Org




More information about the Python-list mailing list