57

How I explain OOP to a Data Scientist in 5 minutes

 5 years ago
source link: https://www.tuicool.com/articles/hit/7v6V3mZ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How I explain OOP to a Data Scientist in 5 minutes

Every time you use Pandas, you are using an object…

YbaYjeY.jpg!webJJVVzqI.jpg!web

When 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!

Me: Can you write me a character who has Name, Health, Mana and Level?

name = "Jason"
health = 100
mana = 80
level = 3

Me: Now I want to add a monster to the game :D

hero_name = "Jason"
hero_health = 100
hero_mana = 80
hero_level = 3
monster_name = "Techies"
monster_health = 20
monster_mana = 0 
monster_level = 1

Me: What if this time I want 10 monsters?

hero_name = "Jason"
hero_health = 100
hero_mana = 80
hero_level = 3
monster_1_name = "Techies"
monster_1_health = 20
monster_1_mana = 0 
monster_1_level = 1
monster_2_name = "Sand King"
monster_2_health = 120
monster_2_mana = 20 
monster_2_level = 3
...
monster_10_name = "Chaos Knight"
monster_10_health = 150
monster_10_mana = 50
monster_10_level = 10

LH: This doesn’t make sense…

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 = level

What 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 = 0
monster_2.level += 1

LH: Cool man! Is that all for OOP?

Me: Let me tell you more about OOP next week!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK