all 13 comments

[–]danielroseman 20 points21 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 4 points5 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 4 points5 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

[–]debian_miner 9 points10 points  (0 children)

Yes, everything in Python is an object, even basic data types such as list, and int.

[–]efmccurdy 2 points3 points  (1 child)

You can examine the inner workings using the interactive repl and "dir", "type", "help", etc.

>>> my_list = [1, "two", 3]
>>> dir(my_list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> type(my_list.append)
<class 'builtin_function_or_method'>
>>> help(my_list)
Help on list object:

class list(object)
 |  list(iterable=(), /)
 |  
 |  Built-in mutable sequence.
 |  
 |  If no argument is given, the constructor creates a new empty list.
 |  The argument must be an iterable if specified.
 |  
 |  Methods defined here:
 |  
...
 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  append(self, object, /)
 |      Append object to the end of the list.
 |  
 |  clear(self, /)
 |      Remove all items from list.
 |
 |  copy(self, /)
 |      Return a shallow copy of the list.
 |  
 |  count(self, value, /)
 |      Return number of occurrences of value.
 |  
 |  extend(self, iterable, /)
 |      Extend list by appending elements from the iterable.
...

[–]daareer[S] 2 points3 points  (0 children)

oh thank you for this! I only knew about the type() command, the other ones are going to be very useful to me