Java to Python? Learn to think differently about type checking!

The Problem

So recently I posted the following blog post explaining my approach to determining of an value is instance of a custom class. I also posted a reddit question asking if my solution made sense and whether I was missing some cases.

This had become a hard thing to do since it Python pretty much everything is an object and is an instance of a class. It was even harder in Python 3x. At least in Python 2.7 you could check type of an object and you’d get <type ‘instance’>. But, now in python 3.x most values return <class ‘name’> thanks to dynamic types. For example an int value will be <class ‘int’> and an instance of the class Foo will be <class ‘Foo’>.

I was wondering how one could differentiate between these instances and separate them. I needed to do this becase I was writing a serialization library and I needed to handle instances of classes differently.

The Mistake

Coming from a statically typed language mindset, like what you get in Java, I was trying to find a way to determine type of classes. Mostly, because of how they store attributes differently (__dict__ and __slots__). I did find a way. I finally ended up checking if they have either of attributes “__dict_” or “__slots_”. I wrote a function and called it “is_object()”. You can read more about it in the original post here.

The mistake was perhaps not in what I wrote (at least I haven’t found any problem with that so far). But, in how I thought about the solution in python. In Java and perhaps other Statically Typed languages we tend to think about objects and their types. We also tend to think about treating objects based on their types. So for example we write an interface for a functionality such as Engine. Then in another method that we are expecting arguments with those functionalities we limit them to objects of that type. Some how the type Engine becomes now the type to check everything against it if we want to make sure that functionality exists. The key here we check against types.

In languages like python that are dynamically typed, that is some how not possible. Variables don’t have types. The values they are pointing to have them (at least in Python). And they can switch values and therefore change types. What we can and must check for is if they have the functionality that we are interested in. This is called Duck Typing . Read more:

Basically, you have to get used to thinking differently. Don’t think about checking if the value has the write type. Simply jump to thinking about if the value has your function, variable and etc. In my case my solution was some how right. Because I wanted to handle the classes different since they were storing their properties in a dictionary with either of the names __dict__ or __slots__. I was actually doing duck typing. But, perhaps I had a wrong mindset and it took me a while to get over it.

Leave a reply:

Your email address will not be published.

Site Footer