THIS Feels Incredibly WRONG In Python, They Call It: "Monkey Patching"

Поделиться
HTML-код
  • Опубликовано: 16 янв 2025

Комментарии • 35

  • @Migoyan
    @Migoyan 2 года назад +15

    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).

    • @mage1over137
      @mage1over137 2 года назад +2

      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.

    • @Ether_Void
      @Ether_Void 6 месяцев назад +2

      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.

  • @AWriterWandering
    @AWriterWandering 2 года назад +4

    It occurs to me that this is the closest thing Python has to externally defined methods in languages like C++.

  • @mage1over137
    @mage1over137 2 года назад +1

    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.

  • @Gruuvin1
    @Gruuvin1 2 года назад

    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).

  • @lukerichards22
    @lukerichards22 2 года назад

    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.

  • @rje4242
    @rje4242 10 месяцев назад

    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

  • @zangdaarrmortpartout
    @zangdaarrmortpartout 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

  • @dcknature
    @dcknature 2 года назад +5

    I really don't care how it feels in Python, as long as it's useful 👌.
    Thanks for the video lesson 😉👍!

  • @GooogleGoglee
    @GooogleGoglee 2 года назад

    @3:20 how did you increase the spacing between the functions with the selection? Thank you

  • @Kevinschart
    @Kevinschart Год назад

    i just implemented monkey patching without realizing it had a name. SUPER useful!

  • @Kommentierer
    @Kommentierer Год назад

    Feels like shooting with pointers like a C-guy.

  • @alidev425
    @alidev425 2 года назад

    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

    • @Indently
      @Indently  2 года назад

      ruclips.net/video/gcsBlLqWIzo/видео.html

    • @juliust.5650
      @juliust.5650 2 года назад +1

      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 ;-) 😉

    • @alidev425
      @alidev425 2 года назад

      @@juliust.5650 Thank you I really appreciate your help and guidance 👌👌

    • @qwerty_qwerty
      @qwerty_qwerty 2 года назад

      @@juliust.5650 you mean hyphen. :-)

  • @nordgaren2358
    @nordgaren2358 2 года назад

    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!

  • @expertcynos2123
    @expertcynos2123 Год назад

    more interested in you linking out your hoodie please, thats cool af

  • @godowskygodowsky1155
    @godowskygodowsky1155 6 месяцев назад

    Mocking is better. Use dependency injection and a strategy pattern.

  • @Logebroderen
    @Logebroderen 2 года назад

    Great content :) gotta get used to how often you say "go ahead"

    • @Indently
      @Indently  2 года назад +1

      Don't worry mate, every new video I make actually leaves that part out, as much as everybody loved it.

    • @Logebroderen
      @Logebroderen 2 года назад

      I noticed :) and I really mean it: you create great stuff and great explanations

  • @grahampawar
    @grahampawar 2 года назад

    Very informative! 😄👍

  • @michaelhoffmann2891
    @michaelhoffmann2891 5 месяцев назад

    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.

  • @cerealport2726
    @cerealport2726 2 года назад

    I really thought monkey patching might be the practice of senior developers throwing excrement over the code written by junior developers...

  • @budweiser1243
    @budweiser1243 2 года назад +1

    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?

    • @C.Ezra.M
      @C.Ezra.M 2 года назад

      If no exception of any kind occurs, the exit code is 0.

  • @Sandeep-tn8mz
    @Sandeep-tn8mz 9 месяцев назад

    Man, i had never heard of it and it was asked t me in the interview

  • @aeaeaeaeoaeaeaeaeae
    @aeaeaeaeoaeaeaeaeae 2 года назад

    This is terrifying and i love it

  • @valentinebucket4628
    @valentinebucket4628 2 года назад

    terrifying

  • @zapp7746
    @zapp7746 2 года назад +1

    Zapp here, and I was the first person here, remember me

  • @kiwiwelch3620
    @kiwiwelch3620 2 года назад

    Commit