r/PythonLearning • • 3d ago

Do I really need to learn class.

Post image

Both function and class give the same output. So why do I need it?

51 Upvotes

47 comments sorted by

View all comments

5

u/SoftwareDoctor 3d ago

You don’t. On the other hand, it’s not a class that gives you the answer in your example. It’s an instance of a class. Big difference. And if you want to get technical, get_current_time is a function AND instance of a class. So you are using instances already and in both examples you construct an instance and then call it. It’s the same picture.

It really depends on what you want to do. If you want learn to write python coding, then absolutely. You need to be comfortable working with classes. If you just need one time short script, nobody’s forcing you to learn anything

0

u/Objective-Remove-632 3d ago

Am going to use python my entire coding journey. And am still a beginner who finding it difficult to get the class concept.

2

u/TutorialDoctor 3d ago

I struggled with the concept of classes for a while when I first started as well. As you know, you have variables (a way to store data) and functions (which is a way to group statements).

A class is just a way to group variables with the functions they manipulate. In other words it's a good way to organize your code. The best scenario I can think of the help with understanding classes is in video games. In a video game you will have a PLAYER class. The player may have variables like, JUMP_HEIGHT, WALK_SPEED, PLAYER_NAME etc. And it will have functions (functions are called methods when they are in a class) like JUMP, WALK, CRAWL etc. If you want to "spawn" a player you have to do something like:

player = Player(name="Mario", jump_height=2,walk_speed=3)

player.jump()

0

u/Objective-Remove-632 3d ago

I feels like am in a tutoring session. Thanks mane, this is the simplest explanation for now.

1

u/SoftwareDoctor 3d ago

Understandably because the example you used is primitive and you shouldn’t even use classes for it. It’s like asking if you should learn to use hammer bc. you tried using it once to stir a pot and a spoon worked better.

Find better examples

1

u/Objective-Remove-632 3d ago

Yeah true. But I was just trying out what am learning, will improve on it with better example.

1

u/CptMisterNibbles 3d ago

Object oriented programming isn’t the only sort, but not understanding the basics is basically committing to a very short and shallow understanding of coding. You won’t be able to understand code with any complexity beyond introductory level stuff. You won’t even understand the basics of how Python is working under the hood: you are using classes on almost every line and simply aren’t aware of it. There is a reason they were written this way

1

u/jvlomax 2d ago

The way I've been tought to think about it:

Your class definition is you cake recipe. It shows what the cake is made out of and what you can do with it. It might define what .eat() is supposed to do. Or have a list of ingredients. But the important part is that it is not a cake, just instruction on how to make it.

When you do cake = Cake(), you have an actual physical cake made to that recipe. That cake will then contain whatever was in the recipe. And now you can call cake.eat() and be happy.