basically what it's doin is basically the blurp.hello_world doesn't actually call any method, it gets interpreted as a field access. The field access goes to the getattribute dunder method, and the argument fn is the name of the attribute (in this case "hello_world"). After that, it returns a lambda that calls whatever function that is passed into the lambda that is from the python builtins. So getattribute returns a lambda that gets the print function from the builtins, printing whatever the name of the field that was accessed.
Python exposes just about every internal aspect of itself and lets you override everything, so you can make getting an attribute return a function with the attribute name as the argument.
If you left off the (“print”) the result would be <function <lambda>>, but that immediately calls the lambda function and prints the result.
Because Python is secretly a meta language under the hood that will let you implement any other programming language within its runtime and there are infinite ways for you to abuse its trust.
Makes me think of libraries that override __or__ so you can write Python that looks like bash, where you're piping things into other things. I've honestly been tempted to use them, but I don't want to curse the next dev to touch my code base with something so unexpected—it's bad enough I use monads.
This is so c++. It always bugged me to do that but fstrings would be quite messy too and putting them in an array and merging is too excessive. Plus this way also allows pathlib to escape characters that one might forget or not know how to do when trying to handle path strings with variables
Mostly for chaining together functions where one or more of them may raise or return a None, but the equivalent if-else blocks would be tedious to write or hard to read for their sheer number. I've tried using them for dependency injection, but the resulting code seemed so foreign to Python I've decided not to use it.
How do you use monad in python? To use monads in an ergonomic way, you need to be able to create many small-ish continuation functions, and pythons syntax for that is horseshit.
Lambdas and partials work just fine, but a lot of the time I just pass a regular function in.
The returns library also did something clever with list comprehension syntax to emulate do-notation in Haskell. I don't use it much, but it makes long chains look much simpler.
Python gives the programmer a lot of freedom, which also means the programmer must know restraint. This is a perfect example of just because you can, doesn't mean you should, but overwriting __getattribute__ can sometimes be useful in niche cases.
Using it to dynamically swap a builtin function and its parameter to be each other is, obviously, abuse of the freedom the language provides.
Maybe I'm getting kinda old, but... what's the point? Yes, you can do that and yes that's completely valid code. Usefulness cleanliness and readability is another question. The language itself allows you implementng much more cursed things and it's completely on your behalf to create maintainable project with provided tools. Like any other language tbh (But in python it can be much more easily visualized with code snippets like this one)
You can do this because in python, literally everything is an object. Including modules, including class definitions, including functions. And since literally everything is an object, you can dynamically change literally everything including changing things like properties of modules and classes and functions.
When I see a language letting you do things like this, I think to myself: "What a silly language". I know a lot of people like Python, but I wouldn't want to use it for software development, just for helper scripts.
223
u/Haunting_Laugh_9013 Oct 11 '26 edited Oct 11 '26
basically what it's doin is basically the
blurp.hello_worlddoesn't actually call any method, it gets interpreted as a field access. The field access goes to the getattribute dunder method, and the argument fn is the name of the attribute (in this case "hello_world"). After that, it returns a lambda that calls whatever function that is passed into the lambda that is from the python builtins. So getattribute returns a lambda that gets the print function from the builtins, printing whatever the name of the field that was accessed.643
u/BiebRed Oct 11 '26
Because Python is secretly a meta language under the hood that will let you implement any other programming language within its runtime and there are infinite ways for you to abuse its trust.
2
u/trollpolado Oct 12 '26
What font is that?
46
u/eruanno321 Oct 11 '26
In C, array[3] and 3[array] are the same thing by definition.
23
u/Delta-9- Oct 11 '26
Makes me think of libraries that override
__or__so you can write Python that looks like bash, where you're piping things into other things. I've honestly been tempted to use them, but I don't want to curse the next dev to touch my code base with something so unexpected—it's bad enough I use monads.14
u/_killer1869_ Oct 11 '26 edited Oct 11 '26
Python gives the programmer a lot of freedom, which also means the programmer must know restraint. This is a perfect example of just because you can, doesn't mean you should, but overwriting
__getattribute__can sometimes be useful in niche cases.Using it to dynamically swap a builtin function and its parameter to be each other is, obviously, abuse of the freedom the language provides.
8
u/HaroerHaktak Oct 11 '26
It can be one because it can be. Please do not question it.
8
u/sarc-tastic Oct 11 '26
Why not
6
u/fearfulEntity Oct 11 '26 edited Oct 11 '26
Maybe I'm getting kinda old, but... what's the point? Yes, you can do that and yes that's completely valid code. Usefulness cleanliness and readability is another question. The language itself allows you implementng much more cursed things and it's completely on your behalf to create maintainable project with provided tools. Like any other language tbh (But in python it can be much more easily visualized with code snippets like this one)
5
u/KaleidoscopePlusPlus Oct 11 '26
blorp
4
u/Kadabrium Oct 11 '26
why does c++ allow overloading -> but not .
4
u/Yankas Oct 11 '26
You can write horrible code that's just as bad or worse in any programming language.
13
u/chimneydecision Oct 11 '26
On second thought, let us not go to Python. ‘Tis a silly language.
3
u/SoloMaker Oct 11 '26
You can also do this in Lua
3
u/king_escobar Oct 11 '26
You can do this because in python, literally everything is an object. Including modules, including class definitions, including functions. And since literally everything is an object, you can dynamically change literally everything including changing things like properties of modules and classes and functions.
2
u/Professional-Ad-5032 Oct 11 '26
you can do this, doesn't mean you should do this.
2
u/SimpleDisastrous4483 Oct 11 '26
I love it. Python is so hilariously permissive, and this is a very fun way to do dumb things with it
1
u/realmauer01 Oct 11 '26
Because python lets you do basically anything. Fuck around and find out style.
-1
u/lmg1337 Oct 11 '26
When I see a language letting you do things like this, I think to myself: "What a silly language".
I know a lot of people like Python, but I wouldn't want to use it for software development, just for helper scripts.
0
u/PythonNoob999 Oct 11 '26
I lub python
2
u/[deleted] Oct 11 '26 edited Oct 11 '26
[removed] — view removed comment
0
u/[deleted] Oct 11 '26
[removed] — view removed comment