How I explain OOP to a Data Scientist in 5 minutesEvery time you use Pandas, you are using an object…Jason InBlockedUnblockFollowFollowingApr 4When I say Data Scientist, I’m really referring to those who spend most of their time on statistics and analytics, those who build various models and use it to solve business problems.
A few keywords that I always hear from the Data Scientists around me — SQL, R, and Python.
Many terms related to the topic of big data, such as Hadoop, Spark, Pig, and Hive were thrown into daily conversations, but still, I barely heard any of them talking about Object-Oriented Programming (OOP).
Although it is not a must for a Data Scientist to know what OOP is, I thought it is still great if they have at least a rough idea of what OOP is.
So a few days ago, during my lunch break, I decided to spend 5 minutes of my time explaining the concept of OOP, to one of my colleagues —LH.
Since Python has always been the favorite language for Data Scientist, I chose to explain OOP to LH using Python.
Here’s how I explained it.
Me: Let’s build a Role-Playing Game with only 1 character using Python today!LH: Okay…?Me: Can you write me a character who has Name, Health, Mana and Level?LH: Sure, a piece of cake.
name = "Jason"health = 100mana = 80level = 3Me: Now I want to add a monster to the game :DLH: Hmmm…Okay…hero_name = "Jason"hero_health = 100hero_mana = 80hero_level = 3monster_name = "Techies"monster_health = 20monster_mana = 0 monster_level = 1Me: What if this time I want 10 monsters?LH: …??hero_name = "Jason"hero_health = 100hero_mana = 80hero_level = 3monster_1_name = "Techies"monster_1_health = 20monster_1_mana = 0 monster_1_level = 1monster_2_name = "Sand King"monster_2_health = 120monster_2_mana = 20 monster_2_level = 3.
monster_10_name = "Chaos Knight"monster_10_health = 150monster_10_mana = 50monster_10_level = 10LH: This doesn’t make sense…Me: Let me show you how OOP can solve this!An object-oriented way of solving this problem is to use an Object — treat everything as an object.
Notice that both hero and monsters have the same properties.
We can have a generic Class called Creature that the hero and monsters share:class Creature(): def __init__(self, name, health, mana, level): self.
name = name self.
health = health self.
mana = mana self.
level = levelWhat is a Class?.A class is like a blueprint of an object.
Now, whenever we want a new monster, hero or any other creatures, we do not have to rename our variables or keep creating multiple properties for each of them.
Using the Creature Class that we just declared, as a blueprint, we can create new object easily:hero = Creature("Jason", 100, 80, 3)monster_1 = Creature("Techies", 20, 0, 1)monster_2 = Creature("Sand King", 120, 20, 3).
monster_10 = Creature("Chaos Knight", 150, 20, 3)To access the properties of the objects, we can simply do this:hero.
name = "James"monster_1.
health = 0monster_2.
level += 1LH: Cool man!.Is that all for OOP?Me: This is just part of what OOP offers, there are actually many more.
Me: Let me tell you more about OOP next week!LH: Sure!.Looking forward to that!.