Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Reflection

00:00 Welcome to this last lesson in Object-Oriented Programming in Python versus Java. In this lesson, you’ll learn about reflection: how an object can learn information about itself.

00:13 In Java, we can use an object’s .getClass() method to find out what class it belongs to. In Python, we use the type() function. So, for example, if we go back to our Car example, you create a Car object.

00:37 I can print out the value of the type() function call on my_car and it tells me that it is of <class 'car.Car'>, the Car class from the car module.

00:53 Instead of using Java’s instanceof operator, in Python we would use the isinstance() function, which you would have seen in the last lesson as well.

01:06 For example, I can ask

01:12 “Is my_car an instance of Car?” And we get that it’s True. But remember, Car in the last version you looked at inherited from Device, so I can also ask “Is my_car a Device?” And we can get that that’s True as well.

01:38 Java has class static methods: getFields(), which gives you an array of all of its public fields, and getDeclaredMethods(), which will return the public methods. To get similar information from a Python class, you would use the directory function, or dir().

02:00 So I can say “Let’s print the value of dir(),” call it on my_car, and we get this collection of all of the attributes and fields—the ones we’ve defined, and the ones we’ve inherited that can be used for this Car object.

02:24 If you want to know more about a specific attribute, there is the get attribute function, getattr(), which can tell you information about that attribute.

02:37 So I could say, for example, “Print the result of getattr() taking two parameters, my_car followed by one of its attributes. So, for example, I can ask about my_car’s .voltage attribute, or I could ask about a method.

03:10 Lastly, we can actually use reflection to call methods. getattr() doesn’t distinguish between methods and variables, so we can use another function called callable(), which will tell us if the attribute we’ve provided is something that you can call—for example, a function or a method.

03:34 For example, I could loop through everything that dir() gives us, for method_name in, the directory of information for my_car.

03:50 I get the attribute referred to by that method_name,

03:58 and if it’s actually callable, I will go ahead and print that out. So this will give me all of the callable things—all of the methods that my_car can use.

04:10 So from the directory listing of the object, we can now reduce that to those things that we can actually call. And we can actually use that to call a particular object’s method through this process of reflection.

04:27 So what we’re going to do is we’re going to search through the list that we just saw from the previous loop. We’re going to look for the .__str__() method and then call it.

04:39 So we’re going to set up the same loop.

04:46 Now, the collection generated from the directory listing is just a collection of strings, so a method name is just a string. The actual attribute, the actual method it refers to, we’re going to have to obtain through the getattr() function call.

05:03 So from my_car we will find the attribute named method_name.

05:11 So at this point, attr could be a field, it could be a method. We want to make sure it’s a method, so if it is callable—if the attribute is something callable—we want to see if it is the .__str__() method.

05:27 So we’ll check the '__str__' variable method name. If method_name equals the '__str__' method name, then the attribute attr is callable, and so we will call that. It’s a function.

05:47 It doesn’t need any parentheses, since we are within the class. It doesn’t need any parameters in the parentheses, excuse me.

05:56 And it calls our string function. Car silver : fusion : 2018. And so there are just a few techniques of using the object to find out information and use information by itself, called reflection. Next, we’ll wrap everything up.

Become a Member to join the conversation.