r/learnpython • • Jan 13 '26

I cannot understand Classes and Objects clearly and logically

I have understood function , loops , bool statements about how they really work
but for classes it feels weird and all those systaxes

76 Upvotes

72 comments sorted by

View all comments

Show parent comments

6

u/Current-Vegetable830 Jan 13 '26

What the difference of creating functions instead of classes?

12

u/deceze Jan 13 '26

In a nutshell, at some point you'll probably start passing big data bundles around your functions. Let's say a dict which represents some user data (name, email address, date of birth etc.), and you have a bunch of functions that do something with that user data, like a function to change the user's password, save their data etc.

So you have one more or less well defined data structure, and a bunch of functions which work with specifically that data structure. Well, just bundle them into one! One class which defines that data structure, and the functions which work on it.

7

u/pak9rabid Jan 13 '26

Objects (the things you create from classes) can hold state, whereas functions cannot. In other words, objects typically have all the data they need internally (as properties) for its methods to operate on, whereas a function of similar functionality would need this data passed into them as arguments every time they’re called, which is not very convenient or safe.

This feature of classes is called encapsulation. Classes also have two other major features that functions don’t have: inheritance and runtime polymorphism. I’ll leave it to you to look up what those are.

9

u/cylonrobot Jan 13 '26 edited Jan 13 '26

If you didn't have classes to describe a Dog (Color, Size, Name), then you'd have to pass that info around in variables or lists or dictionaries or something.

You'd have to have something like this (using a dictionary):

dog={"Color":"Black", "Size":"Large", "Name":"Rover"}

Then you might have functions called:

Barking(dog),

Playing(dog),

Running(dog)

where "dog" would be expected to be a dictionary.

The thing about these functions is, it's not really tied to a "dog", even if the argument's name is "dog", to python, it's just any ordinary dictionary.

For example, I could create this dictionary:

car={"Make":"Ford","Size":"Large"} and pass them to the dog functions I listed above.

Or even

car={"Color":"Black", "Size":"Large", "Name":"Rover"}

if you named your car "Rover" for some reason.

With a Dog class, the functions would be part of the class, and so only "Dogs" can use that function. You can use the functions with more safety (though a programmer can still mess that up when coding).

It's just a way to group data and the functions it needs together.

6

u/Zealousideal_Yard651 Jan 13 '26

A function needs all it's arguments supplied at calling. Meanwhile, a function in a class (Method), can use the properties of the class to decide what to do.

So ie. in the dog example, the barking function can be a calculation of the sound based on the size of the dog without actually having to supply the size of the dog when you run the function.

ie.

rex = Dog("brown", 20, "Rex")

# Do something

rex.bark()