r/learnpython • ¯\_(ツ)_/¯ • Feb 13 '21

HOW HAVE I BEEN SO BLIND!!!!

IT IS ALL OBJECT....

EVERYTHING IS OBEJCT!!!!

EVERYTHING!!!!!

OH MY GOD!!!!

IT'S ALL OBJECT!!!!!!

AHHHHH!!!!

1.3k Upvotes

200 comments sorted by

View all comments

Show parent comments

43

u/certainly_imperfect ¯\_(ツ)_/¯ Feb 13 '21

it hit me like a truck. i was just sitting there and then all of a sudden, "Wait...It's all object"

20

u/DataDecay Feb 13 '21

Some people in the community will sometimes bash OOP, which makes me double take since you know that is Python.

2

u/Nobody_gets_this Feb 13 '21 edited Feb 13 '21

Tbf, if you have to define classes yourself, it‘s a very different use case.. Before you bash me, I know Python for about half a year now.

Edit: Downvote me instead of helping me to understand this. Thanks.

12

u/hassium Feb 13 '21

Tbf, if you have to define classes yourself, it‘s a very different use case..

Maybe you aren't doing class inheritance right? IME Python makes defining custom and especially child classes a breeze.... Apart from some namespace quirks but easy to work with once you wrap your head around it.

Maybe I'm not super clear what you mean by "it's a very different use case" though?

Edit: Downvote me instead of helping me to understand this. Thanks.

You're being downvoted cause you didn't present a clear issue or problem, just a vague complaint.

3

u/Nobody_gets_this Feb 13 '21

Well yeah, I didn’t properly express myself.

What I meant by „It’s a very different use case“ is: You dont have to mess around with naming things „self.innit“ or something to the extent. You just use the module.

You dont have to define anything, don‘t have to properly give parameters and such. You just use the module(s)

I believe that’s what people mean when they talk bad about OOP. You have to „define“ so much more or just.. Think differently about stuff. It‘s not as easy as using the pre-defined objects.

3

u/darkecojaj Feb 13 '21

Java is the same (except for primitives). I kind of just made that realization myself, i knew they were but not how useful it can be.

0

u/[deleted] Feb 13 '21

[deleted]

9

u/Allanon001 Feb 13 '21

They are objects:

>>> print(len.__name__)
len
>>> print(sorted.__name__)
sorted

4

u/[deleted] Feb 13 '21

Both sorted and len are functions (callable objects) that take arguments that are sortable and length-able objects... not all objects are sortable or length-able, but enough of them are for it to make more sense to have a general interface pattern rather than on that has to be implemented on an class-by-class basis

1

u/causa-sui Github username Feb 14 '21 edited Feb 14 '21

a general interface pattern

This is the interface pattern: len() tries to create an iterable of the object and if successful, returns the length of the iterable. If you haven't implemented that in the data model of your object then it will die with the object is not iterable exception. If you have implemented it, it will work.

On the other hand, if there were a .len object property but not a corresponding __iter__ method in the same base object class, your general interface is arguably broken.

I recognize that different languages handle this differently, but it's not like this is black-and-white obviously better.

1

u/[deleted] Feb 14 '21

First, no one said it’s obviously better, just that it made more sense in the context of Python than the alternative, which it absolutely did.

It made more sense because your explanation of len is a little bit off... Python does not exactly “try to create an iterator” (that’s possible of an object that implements the iterator protocol, which includes iterables that have no size bound), instead it checks:

  1. if the object is a custom class that satisfies collections.ABC.Sized by explicitly implementing class.__len__.

  2. If the object implements the sequence protocol, which it may satisfy by implementing the mapping protocol, which is a superset.

In CPython all of this is encapsulated by successfully calling PyObject_Size on a given PyObject and getting back a positive integer.

The reason why the interface pattern was the local optimum for Python is precisely that ability to explicitly “opt-in” to behaving like a builtin by implementing a sufficient subset of the “magic” methods defined by the object model. With len there’s really only one such method, but in many cases (like iter) the protocol could be satisfied by a number of different paths... in both cases it was in fact “obviously better” for Python to provide a common interface and let everything that quacked like a duck be a duck, but sure that wouldn’t be a sensible choice in languages without its rich (and at times infuriatingly complex) object model.

6

u/DataDecay Feb 13 '21

Functions are objects.

>>> def foo(): pass
>>> foo.__call__

4

u/tangerinelion (C++ Software Eng.) Feb 13 '21

Right, see the OP. Everything is object.

1

u/occipitalshit Feb 13 '21

You got hit by the truck? Very few people in the Movie get hit by the truck....

1

u/DataDecay Feb 13 '21

Best alternate ending ever!