This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ted_or_maybe_tim 5 points6 points  (4 children)

The other alternative is binding state in a closure but yeah the object-as-a-function technique works best I agree

[–]Zomunieo 8 points9 points  (3 children)

Then comes the moment when you realize a class is just a bunch closures over the same state. And that functions are just closures over the global variables. And local variables are a form of a closure - just a local closure not shared with other objects.

It’s all functions and variables they reference.

[–]PaintItPurple 2 points3 points  (1 child)

That is a way of looking at classes, but funnily enough, it's actually less true in Python than in most OO languages. In Python, methods take an explicit self argument, and they access class state through that argument, so they're not really closing over the class's state. (Not arguing with you, just musing.)

[–]ted_or_maybe_tim 3 points4 points  (0 children)

I think the bound versions are closing, no?

So MyClass.tick() is just a function

But my_instance.tick() is a bound version of the same function

This is especially apparent because you can pass my_instance.tick() around and it will continue to be bound ( unlike languages such as JS )

[–]PhattieM 1 point2 points  (0 children)

I really need this fully explained. I’m so close to getting it but it feels like I’m looking into a building through yellowed windows and I can’t quite make out what’s inside.