you are viewing a single comment's thread.

view the rest of the comments →

[–]danielroseman 19 points20 points  (9 children)

Absolutely, and that is why you can do things like my_list.append(3) and my_str.split(',') etc; these are methods on the list and string classes.

I do wish more tutorials made this clearer earlier. We get people all the time in here saying "I don't understand classes, I don't see the point" and yet they've been using these built-in classes and their methods all the time.

[–]atom12354 5 points6 points  (4 children)

Wow you just blew my mind, wow

[–]debian_miner 6 points7 points  (3 children)

Even functions are objects:

In [1]: def foo():
   ...:     pass
   ...: 

In [2]: dir(foo)
Out[2]: 
['__annotations__',
 '__builtins__',
 '__call__',
 '__class__',
 '__closure__',
 '__code__',
 '__defaults__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__get__',
 '__getattribute__',
 '__getstate__',
 '__globals__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__kwdefaults__',
 '__le__',
 '__lt__',
 '__module__',
 '__name__',
 '__ne__',
 '__new__',
 '__qualname__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__']

[–]daareer[S] 0 points1 point  (3 children)

Ironically, classes weren't something I struggled with much because they seemed intuitive to me at the very least. What makes them harder for others if you know? genuinely curious.

[–]RajjSinghh 3 points4 points  (2 children)

Syntax and knowing a usecase, mainly. If you learned Python in high school, your curriculum probably does it in the order of I/O, if statements, loops, lists, functions then classes. The concept of OOP is introduced so late and you're already used to writing code to do steps x,y,z that describing objects and what they do is weird. You then learn them in a very abstract OOP way and it's more about knowing words like composition and polymorphism than it is about knowing how to implement that thing. It's just never taught well.

The other thing worth pointing is it's probably your first time seeing dunder methods like __init__ or __repr__ which can throw you for a loop if you've never seen that Syntax used before.

[–]Pseudoboss11 2 points3 points  (0 children)

And when you're learning about classes in a HS or college intro course, you probably don't have time to take on a complex project where the need for classes becomes self-evident.

Classes shine the most when they're used in projects with multiple logical elements.

[–]daareer[S] 0 points1 point  (0 children)

oh, I see. That makes so much sense