Static Typing in Python

Jacek Generowicz jacek.generowicz at cern.ch
Fri Mar 12 11:38:28 EST 2004


Jacek Generowicz <jacek.generowicz at cern.ch> writes:

> Peter Maas <fpetermaas at netscape.net> writes:
> 
> > Premshree Pillai schrieb:
> > > How do I force static typing in Python?
> > 
> > You have to enforce it by code instead of declaration, i.e. you
> > have to do runtime type checking.
> 
> Just what do you understand "static typing" to mean ?

I ask because I am prepared to accept that you have a different
working definition of "static typing" from mine, but people who
actually want static typing typically want it because they think it
gives them the following advantages:

a) type errors caught at compile time,

b) faster program exectution.

The code you showed:

> > class doesTypeChecking:
> >      def __init__(self):
> >          self.__dict__["aString"] = ""
> >          self.__dict__["aFloat"] = 0.0
> > 
> >      def __setattr__(self, attr, value):
> >          if type(self.__dict__[attr]) != type(value):
> >              raise ValueError, "Type mismatch for attribute %s\n" % attr

will catch no type errors at compile time, and will slow down
execution, so I suspect that Premshree will be disapponinted with it.


(BTW, my definition of "static typing" is "type checking is done at
compile time" ... your example really looks like dynamic typing to
me ... albeit with some restrictions on attribute types.)


Premshree: Python is dynamically typed. There is no way to enforce
           static typing. There is something called "pychecker" which
           might be of some help to you. Why do you think that you
           want static typing in Python ?



More information about the Python-list mailing list