detecting param type
Jason Cunliffe
jasonic at nomadicsltd.com
Wed Oct 25 17:31:24 EDT 2000
From: Mike Fletcher <mfletch at tpresence.com>
To: 'Jason Cunliffe' <jasonic at nomadicsltd.com>
Sent: Wednesday, October 25, 2000 3:54 PM
Subject: RE: detecting param type
> import types
>
> if type(date) != types.StringType:
> date = str( date )
Mike
Thanks very much.
This solves my immediate problem.
...........
Since I posted my query, I checked against David Beazley's excellent book
'Python Essential Reference'
He says on p.68:
<quote>
Classses, Types and Membership Tests
"Currently there's a separation between types and classes. In particular,
builtin types such as lists and dictionaries cannot be specialized via
inheritance, nor does a class define a new type. Infact, all class
definitions have a type 'ClassType', while all class instances have a type
of InstanceType. Thus the expresssion
type(a) == type(b)
is true for any two objects that are instances of a class (even if they were
once created by different classes).
To test for membership in a class, the builtin function isinstance(obj,
cname) can be used. This function returns true is object obj belongs to
class cname or any class derived from cname.
Simliarly the builtin function issubclass(A, B) returns true if if the class
A is a subclass of class B.
It should be noted that the isinstance() function can be used to perform
type checking against any of the builtin types. For example:
import types
isinstance(3, types.IntType) #returns 1
isinstance(3, types.FloatType) # returns 0
This is the recommended way to perform type checking with builtin types, as
the distinction between types and classes may disappear in a future
release.
</quote>
Jason
More information about the Python-list
mailing list