seyed seyed - 2 years ago 124
Python Question

full type of object in python

I'd like to print the full type of an object

for example:

# 1
print full_type(['a','b','c']) # output: "list of str"

# 2
x = book.objects.filter(user=user) # Django Query Set
print full_type(x) # output: "QuerySet of book"

Answer Source

Container objects in Python can contain any type of object, and even mixed types. This is different from statically typed languages where a container must be declared along with the type of object it contains. It doesn't really mean anything to ask what the "full type" of an object is in Python. In other words, the only difference between a list instance that contains integers and another instance that contains strings is their contents.

However, if you really want a function to print this out, this will do it:

def full_type(obj):
    return "%r of " % type(obj) + ','.join('%r' % t for t in set([type(o) for o in obj]))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download