Reflection in Python:

It refers to the ability of a code by which it can examine attributes about objects which might be there as parameters to a function. We can use reflection to write a recursive reverse function which will work for lists, strings, and any other sequence supporting concatenation and slicing. Suppose an object is a reference to a string, Python will return the str type object which we can use in data science.

python data science

Reflection Enabling Functions:

  1. type and isinstance – type() helps to figure out the type of variable in the program in runtime. On the other hand, isinstance() checks whether the object is an instance of classinfo class.
  2. Callable – Anything which we can call is callable. A class is made callable by providing __call__() method. If the object is callable the callable() method returns True, otherwise False.

python data science

Callable being there in OOP

  1. Dir – This returns a list of valid attributes of an object. If an object contains the __dir__() method, then it will be called and it must return the list of attributes. In case the object does not have __dir__() method, it tries to find the information from the __dict__ attribute and type object. In such a case the list returned may be incomplete.

OUTPUT:

  1. Getattr – This method returns the value of named attribute o an object. If the value is not found then, it will return the default value provided to the function. The method getattr takes three parameters object, name and default. Default is optional.

If you want to leaern more about reflection in python for data science, then you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.