OOPs We just Changed the Object’s Class: Python 3

leangaurav
Industrial Python
Published in
3 min readJul 20, 2019
This is an Image.. Code comes later :-P

For foodie developers… before starting we have some Chicken Wings also.

Ribs & Wings/ Alpha / Wikimedia Commons

Not able to get what we are going to do? We will first create 2 classes in python. Then create their objects and then interchange their classes.

Well !! Is it Possible?

Yes.. at least in Python.

But why will we do that?

Well just for the sake of fun, knowledge and cos we can do that.

These are the two classes we are going to use throughout. Just read the below code once(or twice or thrice…) and then we can start flying.

Above code creates 2 classes with one common function fly() . Each has a constructor __init__() but for ChickenWings there’s an attribute price . The __str__() overload also returns different data for the objects of each class.

In the test code we will create 2 objects, one for each class type. And since we haven’t messed up with the classes yet, it works as expected.

Better to write the two classes in an importable script(I am using wings.py) so that we can use it in both interpreter and another script or maybe import in jupyter-notebook as well. Do whatever suits you.

The __class__ attribute and type() function

In Python each class has an attribute __class__ that refers to the class or type of that object.

Clearly type() returns the __class__ attribute of the class.

Now output of above code should make sense.

Now lets think of the impacts of interchanging the class of the two objects.

Object’s State/ Instance Variable

Since the attributes assigned to the object (that is the ones we created in the __init__ constructor) are maintained withing a dictionary within the object itself, hence it has no impact on them since when the object was created, the correct __init__ method was invoked to create the instance attributes.

So there should be a price attribute in the ChickenWings object and no attribute in the Wings object.

And that’s why we have an AttributeError from the last print statement.

So no impact on instance/object attributes created before the class swap.

Method Calls/ Class Attributes

All methods and attributes are maintained inside the class. And this means that changing the class will change the method which gets called. So we can easily guess the output for this one.

More intuition comes from the fact that each function call translates to something like this.

w.fly() => w.__class__.fly(w)

Which tells us why function calls change( and that’s why the self argument in all the class methods… the object goes to the function).

Conclusion

Classes in python maintain their definition via some attributes (ones starting and ending with double underscores). Changing these attributes can have some serious side effects.

Find code files here:

https://github.com/leangaurav/BlogCode/tree/master/ChangeClassChickenWIngs

--

--

leangaurav
Industrial Python

Engineer | Trainer | writes about Practical Software Engineering | Find me on linkedin.com/in/leangaurav | Discuss anything topmate.io/leangaurav