top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

What is the Pythonic way to determine the type of an object?

+1 vote
51 views

What is the Pythonic way to determine the type of an object? Are there multiple valid ways, and when should each be used?

We have obj.__class__, an attribute bound to the object's class. Or is it? When is that true, and when should we not rely on it?

We have type(obj), calling the constructor for the type type in order to get a reference to the type of obj. Or is it? When is that true, and when should we not rely on it?

Are there other ways to get at the type of a Python object? What reasons are there to choose or avoid them?

posted Dec 16, 2013 by Majula Joshi

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button

Similar Questions
0 votes

I am analysing designing an abstraction layer over a select few NoSQL and SQL databases.

Specifically:

  • Redis, Neo4j, MongoDB, CouchDB
  • PostgreSQL

Being inexperienced; it is hard to know a nice way of abstracting search. For conciseness in my explanation, think of Table as being table, object, entity or key; and name as being name or type.

Maybe res = Table.name.search()

Or on multiple Table:
`res = AbstractDB().AbstractSearch(

)`

Then: res.paginate(limit=25, offset=5)

Or if you want all: res.all()

And additionally borrow/alias from a relevant subset of PEP249, e.g.: fetchone and fetchmany

Will open-source this once it's of sufficient functionality. Any suggestion

+3 votes

What information used by MME to determine an appropriate APN-OI and S8 protocol type (PMIP or GTP) when the MME and PDN GW are located in different PLMNs?

And if the HSS provides the identity of a statically allocated PDN GW, or the HSS provides the identity of a dynamically allocated PDN GW and the Request Type indicates "Handover", no further PDN GW selection functionality is performed means PDN Gw is chosen which is mentioned in subscriber information by HSS ?

0 votes

The type() builtin according to python docs, returns a "type object".
http://docs.python.org/2/library/types.html

And in this module is bunch of what I assume are "type objects". Is this correct?
http://docs.python.org/2/library/functions.html#type

And type(), aside from being used in as an alternative to a class statement to create a new type, really just returns the object class, doesn't it?

>>> import types
>>> a = type(1)
>>> b = (1).__class__
>>> c = int
>>> d = types.IntType
>>> a is b is c is d
True
>>> 

If type() didn't exist would it be much more of a matter than the following?:

def type(x): 
 return x.__class__

What is the purpose of type()?
What exactly is a "type object"? Is it a "class"?
What is the purpose of the types module?

I understand the purpose of isinstance and why it's recommended over something like (type(1) is int). Because isinstance will also return True if the object is an instance of a subclass.

>>> class xint(int):
 def __init__(self):
 pass

>>> x = xint()
>>> type(x) is int
False
>>> isinstance(x, int)
True
>>> 
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...