>python Python 2.4 (#1, Dec 4 2004, 20:10:33) [GCC 3.3.3 (cygwin special)] on cygwin Type "help", "copyright", "credits" or "license" for more information. >>> class Foo(object): ... pass ... >>> f = Foo() >>> f is Foo # false. f as an instance of Foo, not Foo itself False >>> type(f) == Foo True >>> type(f) is Foo True
WARNING: Doing this may result in brittle programs. What, say, later on you want to make a wrapper around Foo? then type(f) == FooWrapper, not Foo. If there are particular attributes an object must have, check thay are there with hasattr. Then the type of f can be changed to whatever later on, and your program has more of a chance of working without other changes. Cheers, Carl. On 02/02/06, Andrew Errington <[EMAIL PROTECTED]> wrote: > Hi, > > This is a question for Python wranglers: > > If I make a class, how can a programmatically detect that any given > object is of that class type? > > e.g. > > class myclass: > pass > > Now, given an object 'foo' how can I say "If foo is of type myclass then do > something". > > I know how to use type(), but in this case type(foo) returns <type > 'instance'>, but any class returns this same result. > > (yes I did Google, and didn't like what I found) > > Andy >
