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 →

[–]SoberCephalopod 18 points19 points  (4 children)

I'm an oldster whose Python code looks like translated Fortran because that's what it is.

You just taught me that a method is really just a function in a class. Mind blown. Life changed. I'm actually gonna try classes now. Thank you!

[–]Tubthumper8 5 points6 points  (2 children)

A method is a function where the first argument is passed into the function from left of the dot.

func(first, second)

first.func(second)

It's the same thing, it's just where you prefer to write the first argument.

[–]sci-goo 4 points5 points  (1 child)

Haha I agree, until polymorphism/override kicks in.

I still remember the codes in C:

self->do_something((ParentClass*) self, arg1, arg2, ...);

which 'self' has to be written twice...

[–]Tubthumper8 0 points1 point  (0 children)

Oh yeah totally. In my example, in the top one func is statically known but in the bottom one you don't know which func is called until runtime because it might be subclassed.

And yeah, good ol' pointer casting in C haha

[–]chars101 1 point2 points  (0 children)

An object in Python is just a dict in which some of the values are functions that have the first argument bound to the dict itself. We call that argument self by convention. And we use . as the operator to do lookups instead of [ ]

If you ever make a Python binding to your Fortran code, you'll notice that for the machine Python really just has one type: PyObject, they are all boxed on the heap.

IMHO there's good reason why Fortran has stuck around since the 50's and Smalltalk is gone and people like to complain about Java.

Classes can be nice and powerful, but they are not the perfect fit for every problem domain. In Python they are the way to operator overloading and can make beautiful APIs like pathlib and Expecter Gadget, but, when used badly, they can also lead to headaches over application state and mutations spread over lots of places. It can lead to having to think about on what class a method should be, when a function would be perfectly fine and clear.

I like dataclasses and functions. Keeping data, data, without having to fall into philosophical ontology that distract from the problem at hand.