A little stricter type checking

Tongu? Yumruk linuxtrooper at yahoo.com
Thu Sep 2 23:26:32 CEST 2004


I have a little proposal about type checking in python. I'll be glad
if you read and comment on it. Sorry for my bad english (I'm not a
native English speaker)

A Little Stricter Typing in Python - A Proposal

As we all know, one of the best things about python and other
scripting languages is dynamic typing (yes I know it has advantages
and disadvantages but I will not discuss them now). Dynamic typing
allows us to change types of variables on the fly, and frees us from
the redundant work of defining variables, and their types. On the
other hand in languages like C, java etc... where types are strict ve
have the guarantee that the variables will always be the same type
without any change.

For example think that you are writing a utility function for a
library. In python you just define the function name and the name of
the parameters but you can never know the variables that has been
passed from parameters will always function as you planned. If you
want to be sure, you need to explicitly check if the variable has the
correct type. On the other side for example in C you don't need to
check the types of variables since the compiler will complain if there
are incompatible types. But this approach also limits the flexibility
and destroys the advantages of dynamic typing. What we need is a way
to define types of the parameters in a flexible way. So Here it is...

We can use operators like those we use in boolean operations. For
exampla lets assume that we have a function that take one parameter
which needs to be an instance of class foo, or an instance of a
subclass of foo. In current style it will look like that:
def larry(guido):
	<some checks to be sure that guido is an instance of foo or an
instance of a subclass of foo>
	<Actual code>

Well always checking the types of the parameters in the start of every
function is too many redundant work. Instead we can define a syntax
that will check the type information at the beginning of function
declaration, and even before executing any code from that function.
That syntax might look like that:

def larry(guido >= foo):
	<Just actual code, no checking since the interpreter handles it>

And if we have a function that have much more parameters, this kind of
a syntax will really help us to remove redundant code from our
functions. The syntax can be defined by something like:
Symbol	: Definition
==	: The parameter is an instance of a given class for example: d ==
dict
>=	: The parameter is an instance of the given class or one of its
	  subclasses
<=	: The parameter is an instance of the given class or one of its
parent
	  classes

This list can be expanded to include != or even a more fantastic
operator: "in" so that we can check if the parameter has a specific
attribute before executing our function.

The best thing about this approach is that while keeping flexibility
it adds better type checking and it keeps the compatibility with old
code.

So that's all for now... All comments are welcome.



More information about the Python-list mailing list