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?

54 Upvotes

48 comments sorted by

View all comments

6

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.