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

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