What information does type() return for user defined classes?


#1

Question

If the type() function is used on a user-defined class, what information is returned?

Answer

The type() function will return the namespace and class name for the object. The top-level code for a Python program has a namespace of __main__ (unless the code is part of a module). The following code example shows the information returned for a list, a dictionary, and a user-defined class.

class TestClass:
    def __init__(self):
        self.value = 0

    def getVal(self):
        return self.value

mylist = [1,2,3]
mydict = { "A": 1, "B": 2}
myobj = TestClass()

print(type(mylist))
# <class 'list'>
print(type(mydict))
# <class 'dict'>
print(type(myobj))
# <class '__main__.TestClass'>

#2

No, it returns the class