Static Typing in Python

Peter Maas fpetermaas at netscape.net
Fri Mar 12 13:48:56 CET 2004


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. This could be done e.g. within
a class:

class doesTypeChecking:
     def __init__self():
         aString = ""
         aFloat = 0.0

     def __setAttr__(self, attr, value):
         if type(self.__dict__[attr] != type(value):
             raise ValueError, "Type mismatch for attribute %s\n" % attr

Similar code could be used to check parameters of a function/
method.

Have looked at pychecker? It's not part of the python
distribution but it could help here.

Mit freundlichen Gruessen,

Peter Maas

-- 
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail peter.maas at mplusr.de
-------------------------------------------------------------------



More information about the Python-list mailing list