-3

I don't know what type it is. How do I check and print it out to console?

TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • 1
    Was the docs.python.org site down? Was Google down? Did you even try to search here? http://stackoverflow.com/search?q=%5Bpython%5D+type turns up about 500 related questions. Why didn't search work? – S.Lott Nov 04 '10 at 10:26
  • possible duplicate of [Python - Determine the type of an object?](http://stackoverflow.com/questions/2225038/python-determine-the-type-of-an-object) – Qantas 94 Heavy Mar 12 '14 at 04:20

5 Answers5

3

a type is basically a value kind and operations that are valid on that value


type(object) #gives you the type

if you want to test for a type


import types
class foo(object):
    pass

>>> foo is types.ClassType # test if foo is a class
True
mossplix
  • 3,783
  • 2
  • 26
  • 31
2

Call the type() function. See the types module for a list of type names you can use with type().

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
1

type(variable) will tell you what type something is.

Scott
  • 1,477
  • 12
  • 13
1

While you can use type() to perform introspection I have to wonder what you're really trying to accomplish.

In Python it's considered to be far better practice to focus on object behavior ... API ... than on type.

Do you care if the object at hand is a Foo() ... or will some subclass of Foo() or some sort of proxy for Foo() (some object containing a Foo()-like object and delegating the requisite functionality to it) do?

The usual advice I see is to use Python's exception handling ... try to use the desired interfaces as if the object is of the correct type and be prepared to handle the exceptions that may be thrown if the object doesn't support that usage. (Most often that will be a TypeError or AttributeError, of course).

Another option is judicious use of hasattr() to see if the object has the necessary attributes (usually methods) to be used like the desired object. (The main caveat there is that there may be method name collisions with completely incompatible semantics in unrelated classes of objects).

Recent versions of Python have introduced the support for "abstract base classes" (ABCs). This basically allows you to use isinstance() and issubclass() in a way that refers to the intended semantics of the objects being tested rather than on their literal instantiation and class hierarchical artifacts. When used with an ABC isinstance() returns a value which indicates whether class supports the signature methods that are considered definitive of the intended semantics.

You can read more about the most commonly used ABCs in the Python Docs: collections module (section 8.3.6).

Unfortunately it's possible you could still confuse the ABCs if you re-use certain method names which have a conventional meaning in the existing Python core or standard libraries.

For example if you create some UI class and define methods of .next() (perhaps to go with some hypothetical .previous()). Doing so, without further effort, would mean that a call like: isinstance(foo, collections.Iterable) would return True even though the semantics you implemented (presumably something like proceeding to the next page in your UI) are completely orthogonal to those that are intended by the collections.Iterable ABC.

I think it's possible to disambiguate by creating some other abstract base class and specifically registering your class as implementing that abstraction. However, I don't know exactly how that would work.

Jim Dennis
  • 17,054
  • 13
  • 68
  • 116
0
print object.__class__.__name__

Should do the trick.

Khalos
  • 2,335
  • 1
  • 23
  • 38