[Q] Type checking...go further...

Stidolph, David stidolph at origin.ea.com
Mon Jul 19 10:31:51 EDT 1999


You cannot have typed method parameters (Python is typeless), but can only
check the types of parameters within the method.

from types import * #Needed for different types like IntType

def foo(a, b, c):
  if type(a) is IntType:
    print a,'is a integer'
  elif type(a) is LongType:
    print a,'is a long integer'
  elif type(a) is StringType:
    print a,'is a integer'
  elif type(a) is FloatType:
    print a,'is a floating point number'
  elif type(a) is ListType:
    print a,'is a list'
  elif type(a) is TupleType:
    print a,'is a tuple'

or

def foo(a,b,c):
  if not (type(a) is IntType):
    raise "Param a must be integer"
  if not (type(b) is StringType):
    raise "Param b must be string"
  if not (type(c) is ListType):
    raise "Param c must be list"
  ...

Hope this helps,

David Stidolph
Origin

-----Original Message-----
From: Olivier Deckmyn [mailto:olivier.deckmyn at mail.dotcom.fr]
Sent: Monday, July 19, 1999 8:36 AM
To: python-list at cwi.nl
Subject: [Q] Type checking...go further...


Thank you all for your answers !
I finally use isinstance in the methods of my classes...

But to go further, is there a way in python to have typed methods parameters
?
aka :

class MyClass:
    def myMethod(string myString):
        print myString

?

Thanx again all for your help !

Olivier,
happy python newbie.






More information about the Python-list mailing list