The meaning of `this`

https://lemmy.world/post/31027668

Not much experience, but quickly learned .bind() in JS after it switched me to window instead of object.

The key is to not reassign function names to local variables.

const print = obj.toString print() // gives you a bad time
In Python you can use this as a variable name
You can use anything that doesn’t start with a digit or punctuation as a variable name (underscore beginning also allowed) unless it’s a keyword.

_ (sic) as a variable name is often used when a function returns multiple outputs but you only want one

def my_function return 1, 2, 3 _, two, _ = my_function()
Underscore alone is a special variable name and I’m pretty sure anything assigned to it goes straight to garbage collection. Whereas _myvariable is typically use to indicate a “private” class variable or method (Python doesn’t have private so it’s just a convention).

_ can also be used in the python interactive terminal to mean ‘last return value’

Ie:

> 'string' 'string' > a = _ > print(a) string
In Python you can use 🍆 as a variable name.
C supremacy
The source character set is implementation defined.
Just going by the reputation, you probably can do this in JavaScript
~ $ python Python 3.12.10 (main, Apr 9 2025, 18:13:11) [Clang 18.0.3 (https://android.googlesource.com/toolchain/llvm-project d8003a456 on linux Type "help", "copyright", "credits" or "license" for more information. >>> ❗ = 'nah' File "<stdin>", line 1 ❗ = 'nah' ^ SyntaxError: invalid character '❗' (U+2757) >>> ~ $ node Welcome to Node.js v23.11.1. Type ".help" for more information. > const 👍 = 'test' const 👍 = 'test' ^ Uncaught SyntaxError: Invalid or unexpected token >
you might be thinking of Rust.

Edit: oops, guess I was mistaken, you can use most Unicode but emojis are not valid.

That actually seems even more arbitrary. Like, do they just hate fun?

TBF the last two bullet points are verbose descriptions of the thing it means in C++, Java, and Python too. It’s just that in JS, “this” can also be used in other places.

But yeah, in practice, every time I write JS I want to throw my hands in the air and shout “this is bullshit”, but never know what “this” refers to… :D

Yeah that’s fair, though it also discusses that whole prototype thing that JS has going on

Partially unrelated to the meme, but I find it almost malicious how some python keywords are named differently from the nearly universal counterpart of other languagues.

This/self, continue/pass, except/catch and they couldn’t find a different word for switch so they just didn’t implement it.

It’s as if the original designers purposefully wanted to be different for the sake of it.

I read that self as a keyword also has quite a history. It was already used in Smalltalk, an OOP language from the early 80’s.
Smalltalk - Wikipedia

Isn’t self not actually a keyword? Like you can name the first variable in a class method anything and it will behave like self.
You could use “this” instead of “self”. And if you want a lynch mob of Python programmers outside your house, make a push request with that to some commonly used package.
I think there will be a lynch mob of git users outside your house for calling PR as “push request”.
I’ve been wondering about the noise.
TECHNICALLY, there is no such thing as a pull request in git. That’s a Github convention. It’s really a merge request
only github users. git itself doesn’t have PRs, and other forges call them different things. gitlab calls them merge requests, pico calls them patch requests…
You could even choose the name this.
pass and continue are absolutely not equal (pass is a noop, and python has a continue keyword that does what you think), and switch is called match like in many other languages. except is weird though.
“except” is also used in Pascal (or at least the main derivatives of it), but not sure if that’s older than its use in Python or not.
List instead of array, dict instead on object (tho it also has objects)
Iv come to loathe the “pythonic way” because of this. They claim they wanted to make programming easier, but they sure went out of their way to not follow conventions and make it difficult to relearn. For example, for me not having lambdas makes python even more complex to work with. List operations are incredibly easy with map and filter, but they decided lambdas weren’t “pythonic” and so we have these big cumbersome things instead with wildly different syntax.
Tf, who needs lambdas in python?
anyone using map, filter, reduce, or anything in itertools or functools?
I know at least about map() filter() and reduce().

Maybe I’m missing something, but:

W3Schools.com

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

So much Python criticism comes from people who don’t know the language.
I mean, there is a lot wrong with it, but every language has its quirks. Generally I like discussing it’s actual flaws cause it helps me better understand the language.

And switch cases (called match cases) are there as well.

I use lambdas all the time to shovel GTK signal emitions from worker threads into GLib.idle_add in a single line, works as you’d expect.

Previous commenters probably didn’t look at Python in a really long time.

Python Match Case Statement - GeeksforGeeks

Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.

GeeksforGeeks
i mean tbf match case was only added in 3.10
a lambdo which can only contain one expression, and not even a statement is pretty much useless. For anything nontrivial you have to write a separate function and have the lambda be just a function call expression. Which completely defeats the point

Speaking of big cumbersome things with wildly different syntax have you tried a ternary operation in python lately? Omg that thing is ugly. JavaScripts is hard to beat.

uglyTernary = True: if python_syntax == “shit” else: False prettyTernary = javascript_syntax == “pretty” ? true : false

That’s just because you’re used to it. The pythonic ternary is structured like spoken language, which makes it easier to read, especially if you nest them.

Is there an objective argument for the conventional ternary, other than „That’s how we’ve always done it!“?

I don’t read spoken language, but I do read written ones. The problem with python’s ternary is that it puts the condition in the middle, which means you have to visually parse the whole if-block just to see where the condition starts. Which makes it hard to read for anything but the most trivial examples.

The same goes for comprehensions and generators

The conventional ternary is structured like a normal if-else. In fact, in many languages with functional influence, they’re the same thing.

For example, you can write this in Rust:

let vegetable = if 3 > 4 { "Potato" } else { "Tomato" };

If the conventions suck you have to break them. How else can you improve things?

map and filter are almost always inferior to generators and comprehension expressions in terms of readability. If you prefer the former, it’s just because you got used to it, not because it’s better.

PHP naming “::” a Paamayim Nekudotayim is also pretty infamous.

When I’m designing shit, I’m pretty zealous about borrowing terminology from anything even vaguely related to avoid this.

PHP weirdness and inconsintencies never fail to amaze me.

On the bright side, I found my first StackOverflow answer that would fit exactly the same on Linguistic Stack Exchange.

stackoverflow.com/a/59259755

PHP expects T_PAAMAYIM_NEKUDOTAYIM?

Does anyone have a T_PAAMAYIM_NEKUDOTAYIM?

Stack Overflow

Absolutely cursed, lol.

So not only did they decide to randomly include Hebrew in their language, because I guess they were feeling kabbalistic, but they got the Hebrew wrong. In what way does any of that increase usability or even make them look competent?

It reminds me of the INTERCAL manual, which was a joke:

This precedence (or lack thereof) may be overruled by grouping expressions between pairs of sparks (’) or rabbit-ears (").

include Hebrew in their language, because I guess they were feeling kabbalistic

... or because the developers were Israeli: https://en.wikipedia.org/wiki/Zend_(company)#History

Zend (company) - Wikipedia

Yeah, that’s not actually a good reason though, unless you’re developing a Hebrew programming language for Hebrew speakers. I made a bit of a joke about it, yes.

Python does have a switch statement now, actually. And yes, they went out of their way to call it something different - match.

docs.python.org/3/tutorial/controlflow.html#match…

4. More Control Flow Tools

As well as the while statement just introduced, Python uses a few more that we will encounter in this chapter. if Statements: Perhaps the most well-known statement type is the if statement. For exa...

Python documentation
match isn’t just equivalent to switch though, so in this case it actually makes sense to call it something different.

This is very true. Match statements are much more powerful that switch statements in any other language.

For instance:

  • matching objects very specifically
  • if conditions within case statements
  • pulling variables from inside of the object directly.
List and Array terminology also bothers me … Why not just call it an array?
But python lists are not like the base arrays in other languages. They function more like List<> or vector (C++ had to be special) and are named appropriately.
But C++ vector is an array.
Ahh thank you for that information! In all seriousness, I appreciate you correcting my ignorance.

My JS:

Ah, you mean that?

Rust: Borrow handler got mad at you for asking

(I’d assume)

It’s either a reference to an object instance, or the instance itself (depending on whether you specified &mut self, &self or just self).
Don’t forget Self, the type of self.
The last bullet point is not really that common anymore.