Table of Contents
ToggleIn object-oriented programming, reflection refers to the ability to extract information about any object in use. You can get to know the type of object, whether is it a subclass of any other class, what are its attributes, and much more. Python’s standard library has several functions that reflect on different properties of an object. Reflection is also sometimes called introspect.
Following is the list of reflection functions in Python −
We have used this function many times. It tells you which class an object belongs to.
Following statements print the respective class of different built-in data type objects
print (type(10))
print (type(2.56))
print (type(2+3j))
print (type("Hello World"))
print (type([1,2,3]))
print (type({1:'one', 2:'two'}))
Output:
Here, you will get the following output −
Let us verify the type of an object of a user-defined class −
class test: pass obj = test() print (type(obj))
Output:
It will produce the following output −
This is another built-in function in Python which ascertains if an object is an instance of the given class.
isinstance(obj, class)
This function always returns a Boolean value, true if the object is indeed belongs to the given class and false if not.
Following statements return True −
print (isinstance(10, int))
print (isinstance(2.56, float))
print (isinstance(2+3j, complex))
print (isinstance("Hello World", str))
Output:
It will produce the following output −
True
True
True
True
In contrast, these statements print False.
print (isinstance([1,2,3], tuple))
print (isinstance({1:'one', 2:'two'}, set))
Output:
It will produce the following output −
False
False
You can also perform check with a user defined class
class test: pass obj = test() print (isinstance(obj, test))
Output:
It will produce the following output −
True
In Python, even the classes are objects. All classes are objects of object class. It can be verified by following code −
class test: pass print (isinstance(int, object)) print (isinstance(str, object)) print (isinstance(test, object))
All the above print statements print True.
This function checks whether a class is a subclass of another class. Pertains to classes, not their instances.
As mentioned earlier, all Python classes are subclassed from object class. Hence, output of following print statements is True for all.
class test: pass print (issubclass(int, object)) print (issubclass(str, object)) print (issubclass(test, object))
Output:
It will produce the following output −
True
True
True
An object is callable if it invokes a certain process. A Python function, which performs a certain process, is a callable object. Hence callable(function) returns True. Any function, built-in, user-defined, or method is callable. Objects of built-in data types such as int, str, etc., are not callable.
def test():
pass
print (callable("Hello"))
print (callable(abs))
print (callable(test))
A string object is not callable. But abs and test are functions which are callable.
Output:
It will produce the following output −
False
True
True
A class instance is callable if it has a __call__() method. In the example below, the test class includes __call__() method. Hence, its object can be used as if we are calling function. Hence, object of a class with __call__() function is a callable.
class test:
def __init__(self):
pass
def __call__(self):
print ("Hello")
obj = test()
obj()
print ("obj is callable?", callable(obj))
Output:
It will produce the following output −
Hello
obj is callable? True
The getattr() built-in function retrieves the value of the named attribute of object.
class test:
def __init__(self):
self.name = "Manav"
obj = test()
print (getattr(obj, "name"))
Output:
It will produce the following output −
Manav
The setattr() built-in function adds a new attribute to the object and assigns it a value. It can also change the value of an existing attribute.
In the example below, the object of test class has a single attribute − name. We use setattr() to add age attribute and to modify the value of name attribute.
class test:
def __init__(self):
self.name = "Manav"
obj = test()
setattr(obj, "age", 20)
setattr(obj, "name", "Madhav")
print (obj.name, obj.age)
Output:
It will produce the following output −
Madhav 20
This built-in function returns True if the given attribute is available to the object argument, and false if not. We use the same test class and check if it has a certain attribute or not.
class test:
def __init__(self):
self.name = "Manav"
obj = test()
print (hasattr(obj, "age"))
print (hasattr(obj, "name"))
Output:
It will produce the following output −
False
True
If this built-in function is called without an argument, return the names in the current scope. For any object as an argument, it returns a list of the attributes of the given object and attributes reachable from it.
print ("dir(int):", dir(int))
Output:
It will produce the following output −
dir(int): [‘__abs__’, ‘__add__’, ‘__and__’, ‘__bool__’, ‘__ceil__’, ‘__class__’, ‘__delattr__’, ‘__dir__’, ‘__divmod__’, ‘__doc__’, ‘__eq__’, ‘__float__’, ‘__floor__’, ‘__floordiv__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getnewargs__’, ‘__getstate__’, ‘__gt__’, ‘__hash__’, ‘__index__’, ‘__init__’, ‘__init_subclass__’, ‘__int__’, ‘__invert__’, ‘__le__’, ‘__lshift__’, ‘__lt__’, ‘__mod__’, ‘__mul__’, ‘__ne__’, ‘__neg__’, ‘__new__’, ‘__or__’, ‘__pos__’, ‘__pow__’, ‘__radd__’, ‘__rand__’, ‘__rdivmod__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__rfloordiv__’, ‘__rlshift__’, ‘__rmod__’, ‘__rmul__’, ‘__ror__’, ‘__round__’, ‘__rpow__’, ‘__rrshift__’, ‘__rshift__’, ‘__rsub__’, ‘__rtruediv__’, ‘__rxor__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__sub__’, ‘__subclasshook__’, ‘__truediv__’, ‘__trunc__’, ‘__xor__’, ‘as_integer_ratio’, ‘bit_count’, ‘bit_length’, ‘conjugate’, ‘denominator’, ‘from_bytes’, ‘imag’, ‘numerator’, ‘real’, ‘to_bytes’]
print ("dir(dict):", dir(dict))
Output:
It will produce the following output −
dir(dict): [‘__class__’, ‘__class_getitem__’, ‘__contains__’, ‘__delattr__’, ‘__delitem__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getitem__’, ‘__getstate__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__init_subclass__’, ‘__ior__’, ‘__iter__’, ‘__le__’, ‘__len__’, ‘__lt__’, ‘__ne__’, ‘__new__’, ‘__or__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__reversed__’, ‘__ror__’, ‘__setattr__’, ‘__setitem__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘clear’, ‘copy’, ‘fromkeys’, ‘get’, ‘items’, ‘keys’, ‘pop’, ‘popitem’, ‘setdefault’, ‘update’, ‘values’]
class test:
def __init__(self):
self.name = "Manav"
obj = test()
print ("dir(obj):", dir(obj))
Output:
It will produce the following output −
dir(obj): [‘__class__’, ‘__delattr__’, ‘__dict__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getstate__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__init_subclass__’, ‘__le__’, ‘__lt__’, ‘__module__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘__weakref__’, ‘name’]
Key Takeaway: Master Python reflection—inspect objects with type(), dir(), and more—at Vista Academy!
