Difficulty with Classes and Methods

Hi,

I am having great difficulty understanding Classes and methods, they just do not make any sense for me. Ho objects are created and linked to variables, how to call them and assign them…etc. It is a complication that seems unnecessary for me.
Is it a must to know for becoming a data scientist?

On another thought, the syntax itself could be the one that is confusing. When to use self? when to associate variable…etc.

OOP (object oriented programming) is just a design pattern. It can help us organize and structure our code

therein lies the problem, when first learning about classes, the abstraction introduced by objects is not needed for the small program you have and might be confusing. But on the hand, you can’t be thrown into the deep end. That would be overwhelming.

1 Like

I live by that phrase

1 Like

A class represents section of memory, a blueprint. How the memory is layed out is dependent on how you designed your class. The idea is that you put related data and functions together. For example - a video game character might have hp and functions to take_damage or heal_hp.

It would be so tedious to create a new hp variable for every character and to manage them
OOP is the difference between

var badGuy = 100 // hp value
var badGuy_atk = 25 // attack power
var hero = 100
var hero_atk = 50
badGuy -= hero_atk;
hero -= badGuy_atk;
badGuy += 12 // heal after turn

and

var badGuy = Character(100, 25);
var hero = Character(100,50);
badGuy.take_damage(hero.attack);
hero.take_damage(badGuy.attack);
badGuy.heal

Note how the “formula” for characters is hidden behind their design. I don’t need to explicitly know the bad guy heals for 12 or how he attacks even. I just interface with something sensible (the functions that operate on the objects data)

1 Like

You have explained the way you see it.

Now, explain it please the way a person does not know anything about OOP.

Maybe this would be a helpful explanation?
(I found it to be)
https://www.educative.io/blog/how-to-use-oop-in-python

I think it is definitely useful to understand OOP if one is a data scientist. It will certainly help one debug Type errors and why a certain method doesn’t work with a string for example.

2 Likes

Thank you.

I found this link also useful. I think Codecademy can improve in explaining this area.

Object-Oriented Programming (OOP) in Python 3 – Real Python

1 Like

Hello @digitalrockstar76045,

There is a Review section at the end of the lesson. Take a bit of time to experiment and try the bonus challenges. It may help solidify the concepts.

Python Classes | Codecademy

2 Likes

I did all the that. I think reading the concepts from other websites helped me a lot. I think some websites tend to assume you know nothing about the subject so they tend to explain it in different baby steps way so it clicked.

1 Like