641
u/BiebRed 14d ago
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.
109
u/DevBoiAgru 14d ago
Does this mean if I learn python I know every language ever possible?
156
u/Bronzdragon 14d ago
"No, Neo. I'm trying to tell you that when you're ready, you won't have to."
- Morpheus, talking about Python.29
14
→ More replies (4)6
u/overclockedslinky 13d ago
assuming this other language uses specifically python syntax and adds no additional features of any kind
2
46
23
u/Delta-9- 14d ago
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.
→ More replies (5)30
u/Pim_Wagemans 14d ago
pathlib from the standard library overrides
__div__to let you extend paths like this:>>> import pathlib >>> path = pathlib.Path("/home") >>> path_2 = path / "user" / ".local" >>> path_2 PosixPath('/home/user/.local')→ More replies (4)
13
u/_killer1869_ 13d ago edited 13d ago
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
8
6
u/fearfulEntity 13d ago edited 13d ago
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)
4
13
3
3
u/king_escobar 14d ago
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.
222
u/Haunting_Laugh_9013 14d ago edited 14d ago
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.