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

Sdílet
Vložit
  • čas přidán 11. 11. 2022
  • Yes, you read it correctly. In this video I will be showing you a concept which they call "monkey patching" in Python. As crazy as it sounds, it does have some interesting usages!
    ▶ Become job-ready with Python:
    www.indently.io
    ▶ Follow me on Instagram:
    / indentlyreels

Komentáře • 33

  • @Migoyan
    @Migoyan Před rokem +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 Před rokem +1

      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.

  • @AWriterWandering
    @AWriterWandering Před rokem +4

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

  • @dcknature
    @dcknature Před rokem +5

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

  • @expertcynos2123
    @expertcynos2123 Před 10 měsíci

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

  • @mage1over137
    @mage1over137 Před rokem +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.

  • @rje4242
    @rje4242 Před 4 měsíci

    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

  • @Gruuvin1
    @Gruuvin1 Před rokem

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

  • @grahampawar
    @grahampawar Před rokem

    Very informative! 😄👍

  • @lukerichards1188
    @lukerichards1188 Před rokem

    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.

  • @godowskygodowsky1155
    @godowskygodowsky1155 Před 3 dny

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

  • @GooogleGoglee
    @GooogleGoglee Před rokem

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

  • @budweiser1243
    @budweiser1243 Před rokem +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 Před rokem

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

  • @alidev425
    @alidev425 Před rokem

    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  Před rokem

      czcams.com/video/gcsBlLqWIzo/video.html

    • @juliust.5650
      @juliust.5650 Před rokem +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 Před rokem

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

    • @qwerty_qwerty
      @qwerty_qwerty Před rokem

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

  • @zangdaarrmortpartout
    @zangdaarrmortpartout Před rokem

    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

  • @nordgaren2358
    @nordgaren2358 Před rokem

    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!

  • @Kommentierer
    @Kommentierer Před rokem

    Feels like shooting with pointers like a C-guy.

  • @Logebroderen
    @Logebroderen Před rokem

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

    • @Indently
      @Indently  Před rokem +1

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

    • @Logebroderen
      @Logebroderen Před rokem

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

  • @Sandeep-tn8mz
    @Sandeep-tn8mz Před 2 měsíci

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

  • @Kevinschart
    @Kevinschart Před rokem

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

  • @linux2420
    @linux2420 Před rokem

    This is terrifying and i love it

  • @cerealport2726
    @cerealport2726 Před rokem

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

  • @valentinebucket4628
    @valentinebucket4628 Před rokem

    terrifying

  • @zapp7746
    @zapp7746 Před rokem +1

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

  • @kiwiwelch3620
    @kiwiwelch3620 Před rokem

    Commit