Figuring out an object's type

rwgk at my-deja.com rwgk at my-deja.com
Thu May 11 02:10:54 EDT 2000


Try this:

class MyA:
  pass

class MyB:
  pass

a=MyA()
b=MyB()

print a.__class__.__name__
print b.__class__.__name__

However, it seems to me that your "doSomething()" could be a method
of SomePredefinedObject, and "doSomethingElse()" could be a method
of Foobar.

Ralf


In article <8fab7p$4kh$1 at bob.news.rcn.net>,
  "David Allen" <s2mdalle at titan.vcu.edu> wrote:
> Hello,
>
> How can a python programmer figure out the type of an object?  I
> read the library reference, and the only helpful thing was the type
> library
>
> from types import *
> type(someObject)
>
> etc.  But types doesn't seem to distinguish between different types
> of user defined classes.  I.e. if I say:
>
> class Foobar:
> 	pass
>
> class Baz:
> 	pass
>
> x = Foobar()
> y = Baz()
>
> then type(x) == type(y) yeilds 1.
>
> Which kinda sucks.  :)
>
> How do I tell the difference between a Foobar and a Baz?
> Is there some way each class can have a unique typename, so
> I can do something like:
>
> def someFunction(arg):
> 	if type(arg) == SomePredefinedObject:
> 		doSomething()
> 	elif type(arg) == Foobar:
> 		doSomethingElse()
>
> etc.
>
> Any help would be appreciated.
> --
> David Allen
> http://opop.nols.com/
>
>


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list