r/learnpython • • Feb 23 '21

Classes. Please explain like I’m 5.

What exactly do they do? Why are they important? When do you know to use one? I’ve been learning for a few months, and it seems like, I just can’t wrap my head around this. I feel like it’s not as complicated as I’m making it, in my own mind. Thanks.

229 Upvotes

71 comments sorted by

View all comments

Show parent comment

8

u/[deleted] Feb 23 '21 edited Feb 24 '21

what is "self" doing as an argument? I'm familiar with arguments but I'm just starting to grasp classes and am struggling with why self as an argument then gets used as self.type, self.balance, etc etc.

Any help is appreciated!

Edit: y’all are so fucking helpful damn! Thanks everyone

19

u/ChurchHatesTucker .141592 Feb 24 '21

struggling with why self as an argument then gets used as self.type, self.balance, etc

You need self.balance so you can have Unicycle.balance, and since self has to come from somewhere it gets passed as an argument.

At least, that's how I remember to include it.

Fun fact: it doesn't have to be called self. The first argument to init will always function as self. Call it yomamma on your next assignment.

17

u/TheJollyFox Feb 24 '21

I found this explanation by Corey Schafer to be one of the best that finally gave me that 'aha!' moment.

Take a look at his other videos as well, Corey's got a way of explaining that very few can match in the world of Python tutorials.

12

u/[deleted] Feb 24 '21

It's not something that you have to understand to start using classes, but basically the method needs some way of obtaining all of the data attributes (and potentially the other class methods) you have associated with the instance. So when you use dot notation to call my_object.method_name(), the my_object instance is automatically passed as the first argument to method_name(). That's how method_name() obtains access to those data attributes.

5

u/collector_of_hobbies Feb 24 '21

self is the instance that has been created. Mandatory argument. Bonus, can refer to all the variables already stored in self in all the methods.

5

u/BruceJi 📦 Feb 24 '21

When you write a class, it’s just the blueprints to that type of object. When you actually produce one, it’s going to have data there instead of an empty variable. This self keyword lets the class access its own data.

Python passes this in as the first object always. If you missed it out and you have other arguments, the first one is filled with self too. You don’t HAVE to call it self but between Pythonistas, self is used so everyone knows what it is

3

u/Raknarg Feb 24 '21
class Obj:
	def func(self, x):
		pass

o = Obj()

# the next lines are functionally identical
Obj.func(o, 5)
o.func(5)

o.func implicitly passes o as the first argument to func. Canonically we refer to this argument as "self" because we generally pass the object as the first argument, but it doesn't actually matter what we call it

1

u/AinsleyBoy Feb 23 '21

Remindme! 15 hours

1

u/RemindMeBot Feb 23 '21

I will be messaging you in 15 hours on 2021-02-24 14:55:30 UTC to remind you of this link

CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


InfoCustomYour RemindersFeedback