Ironically it's not that Python handles functions differently, it's more that Python handles everything the same way as C handles functions, since in python everything is a pointer.
The funny think is that it is actually possible to do something similar in C because even in C code is just bytes aka. machine code in memory. This makes it possible to rewrite the code at any point in time (small note at the end) so you could read the function store it somewhere else (potentially modify since the offsets are wrong) and replace it with a custom function. A slight modification of this is done by Hyprland (a wayland window manager for x86-64 Linux) to dynamically hook into functions as plugins require them. Note: A few caveats of this is memory maps which may not allow execution (aka program counter point to) certain memory regions and writing at the same time. Sometimes the default because it can leads to Shellcode injection vulnerabilities due to buffer overflows but sometimes desired because this is how JIT compilers like the V8 Javascript engine work.
So because in python modules are objects, and all you need for a module is a file, I have this way of building simple utility scripts that evolve as the complexity grows. I use this technique to create sub class without making a class. At some point everything gets refractored into a class. But it's really a nice way to build a project from scratch.
Where I work, we often have to modify production code, and and restart the production process, which is what we call monkey patching. This is going around the usual process of modifying in a development environment, test, build and release to production (install a proper upgrade).
I did something like this once when I wanted to debug by reading all of the print statements on a script that outputted a LOT of print statements. I just changed the print function to one that wrote the repr string to a file instead. Did the job.
in python you can effortlessly redefine any function, even built in ones, which is a terrific way to trip yourself up: >>> def print(): ... return 1 ... >>> print(2) Traceback (most recent call last): File "", line 1, in TypeError: print() takes 0 positional arguments but 1 was given >>> del print >>> print(2) 2
When testing you should use mocks, it's basically monkey patching with more functionality, less bloat code, and also cleaner Also it is worth noting that the that this work because everything is in the same module. If monkey patching a function from an imported module, a patch will not affect the other imports made in other modules. It's however possible to grab a imported function from another module and patch it in the exact same way as if yhe function was defined in that module
Short quick answer -> It's just a dash + greater-than-symbol. This would be the actual text of the line and what the interpreter sees: def get_data() -> dict: His editor takes advantage of a font that provides special characters/glyphs for common multi-character operators like -> == === != => etc... It looks nice, but can be confusing and/or distracting if your not used to it. Maybe even downright frustrating. Imagine being new to all of this stuff, watching a tutorial, and not even knowing what a symbol means much less that it's just two or three simple characters in reality... You're trying to follow along and type some code that you see and end up looking over your keyboard for a symbol that's not there. It can also be difficult to setup on some systems. So, until your used to the actual characters required for all these various operations, I wouldn't recommend going through all the work to set up pretty code display in your own code editor. The technical term for what these pretty character representations are called is ligature. Emoticons are similar beasts ;-) 😉
You know you can use this to add to the function by saving the function in a variable, first, and then calling it inside the patched function? It looks a bit weird, but that's kind useful. Allows you to either change the imput before calling the original function, or change the output after calling it!
This is indeed wrong, abominable, disgusting - when used in this way and in this context. There's really only one area where it has its place: mocking and stubbing for unit testing. Similar to something like Mockito in Java - but very different from, say, Go, where mocking is done through, for example, dependency injection. TBH, I had never even thought that "yeah, in Python you could (ab)use this anywhere, even in your normal application code, outside of tests". Maybe my brain was just recoiling.
Hey Indently I have a problem, when I was making my discord bot on python I finished but I didnt get the same thing as you I got "process finished with exit code 0" what do I do?
This is possible because function are objects in python. In C the closest thing are function pointers (which is what does python under the hood).
Ironically it's not that Python handles functions differently, it's more that Python handles everything the same way as C handles functions, since in python everything is a pointer.
The funny think is that it is actually possible to do something similar in C because even in C code is just bytes aka. machine code in memory. This makes it possible to rewrite the code at any point in time (small note at the end) so you could read the function store it somewhere else (potentially modify since the offsets are wrong) and replace it with a custom function. A slight modification of this is done by Hyprland (a wayland window manager for x86-64 Linux) to dynamically hook into functions as plugins require them.
Note: A few caveats of this is memory maps which may not allow execution (aka program counter point to) certain memory regions and writing at the same time. Sometimes the default because it can leads to Shellcode injection vulnerabilities due to buffer overflows but sometimes desired because this is how JIT compilers like the V8 Javascript engine work.
It occurs to me that this is the closest thing Python has to externally defined methods in languages like C++.
So because in python modules are objects, and all you need for a module is a file, I have this way of building simple utility scripts that evolve as the complexity grows. I use this technique to create sub class without making a class. At some point everything gets refractored into a class. But it's really a nice way to build a project from scratch.
Where I work, we often have to modify production code, and and restart the production process, which is what we call monkey patching. This is going around the usual process of modifying in a development environment, test, build and release to production (install a proper upgrade).
I did something like this once when I wanted to debug by reading all of the print statements on a script that outputted a LOT of print statements. I just changed the print function to one that wrote the repr string to a file instead. Did the job.
in python you can effortlessly redefine any function, even built in ones, which is a terrific way to trip yourself up:
>>> def print():
... return 1
...
>>> print(2)
Traceback (most recent call last):
File "", line 1, in
TypeError: print() takes 0 positional arguments but 1 was given
>>> del print
>>> print(2)
2
When testing you should use mocks, it's basically monkey patching with more functionality, less bloat code, and also cleaner
Also it is worth noting that the that this work because everything is in the same module. If monkey patching a function from an imported module, a patch will not affect the other imports made in other modules.
It's however possible to grab a imported function from another module and patch it in the exact same way as if yhe function was defined in that module
I really don't care how it feels in Python, as long as it's useful 👌.
Thanks for the video lesson 😉👍!
@3:20 how did you increase the spacing between the functions with the selection? Thank you
Looks like the PyLint extension formatting on save
i just implemented monkey patching without realizing it had a name. SUPER useful!
Feels like shooting with pointers like a C-guy.
Thanks for the video
I'm new in python , have a question, how do you type the arrow (the small vector) before dict in line 4? 1:38
ruclips.net/video/gcsBlLqWIzo/видео.html
Short quick answer -> It's just a dash + greater-than-symbol.
This would be the actual text of the line and what the interpreter sees:
def get_data() -> dict:
His editor takes advantage of a font that provides special characters/glyphs for common multi-character operators like -> == === != => etc... It looks nice, but can be confusing and/or distracting if your not used to it. Maybe even downright frustrating. Imagine being new to all of this stuff, watching a tutorial, and not even knowing what a symbol means much less that it's just two or three simple characters in reality... You're trying to follow along and type some code that you see and end up looking over your keyboard for a symbol that's not there.
It can also be difficult to setup on some systems. So, until your used to the actual characters required for all these various operations, I wouldn't recommend going through all the work to set up pretty code display in your own code editor.
The technical term for what these pretty character representations are called is ligature. Emoticons are similar beasts ;-) 😉
@@juliust.5650 Thank you I really appreciate your help and guidance 👌👌
@@juliust.5650 you mean hyphen. :-)
You know you can use this to add to the function by saving the function in a variable, first, and then calling it inside the patched function? It looks a bit weird, but that's kind useful. Allows you to either change the imput before calling the original function, or change the output after calling it!
more interested in you linking out your hoodie please, thats cool af
Mocking is better. Use dependency injection and a strategy pattern.
Great content :) gotta get used to how often you say "go ahead"
Don't worry mate, every new video I make actually leaves that part out, as much as everybody loved it.
I noticed :) and I really mean it: you create great stuff and great explanations
Very informative! 😄👍
This is indeed wrong, abominable, disgusting - when used in this way and in this context. There's really only one area where it has its place: mocking and stubbing for unit testing. Similar to something like Mockito in Java - but very different from, say, Go, where mocking is done through, for example, dependency injection. TBH, I had never even thought that "yeah, in Python you could (ab)use this anywhere, even in your normal application code, outside of tests". Maybe my brain was just recoiling.
I really thought monkey patching might be the practice of senior developers throwing excrement over the code written by junior developers...
Hey Indently I have a problem, when I was making my discord bot on python I finished but I didnt get the same thing as you I got "process finished with exit code 0" what do I do?
If no exception of any kind occurs, the exit code is 0.
Man, i had never heard of it and it was asked t me in the interview
This is terrifying and i love it
terrifying
Zapp here, and I was the first person here, remember me
Commit