Monday, March 27, 2017

Fun With Python and Monkey Patching

Monkey patching is about replacing attributes of a Python thing with other attributes. Let's use the word "thing" very loosely and have some fun.

Messing With Self

First, the boring case where classes are classes and instances are instances.





Nothing new there. But what is with self? What's so special about it? Let's rebel. Everywhere self is shall be replaced with the word her.



It turns out self is not that special after all. It's just a convention for distinguishing the instance of a class from the class itself. Strictly speaking, self could be this or alice or bob or the gender pronoun of your choice. Politically correct Python for the win!

But seriously, did you ever stop to think about how strange it is when it comes to self and how calls method calls actually work?

ob1.say_stuff()

The ob1 in this case looks like it's the self that say_stuff refers to. Let's twist that call around a little.





Okay, so there's that. Now, be sure of this. The self term absolutely refers to an instance of the class it's being used in ... right? You have to wonder sometimes.




So much for that idea. At any rate, it's safe to say we've abused self enough for now. 


Time For Some Actual Monkey Patching

Here, we play a game about filling in the missing pieces. What if we had a starting class without an __init__.  To make it more interesting, let's NOT set up another class through which to provide instance variables. Something like this.




And who would have thunk. We can actually make this work in spite of the fact that the instance was born without a msg. Time for some setattr() surgery.




Oh heck, let's go gangbusters and just monkey patch a bare naked class together. Here's what that ends up looking like. Heck, we'll do up the instance too. Here it goes.


What else to play with? Ooo, I know!

Direct Assignments And Dictionaries

Python classes seem mutable enough. Maybe you can even directly assign to one method to take the place of the original. 




Awesome! That opens up quite a few possibilities right there.

Okay, one last thought. Python classes and instances have these things called dictionaries that dwell beneath the surface. Dictionaries represent methods and attributes.

Now, here's an idea. say_stuff isn't a member of the instance dictionary. It's a member of the class that the instance is based on. If there no say_stuff in the instance? No problem. Just look it up in the dictionary of the class instead.

Sooooo, what if we exploited the instance dictionary to subvert that expectation.




And yes, that setattr trick that's commented out there works too. And there are other things to say and do with these underlying dictionaries too. However, exploring much further requires  diving into the mysterious world of metaclasses. Let's not go there.... unless you really want to. ;-)

Well That Was Fun!

This is definitely not something you'd likely want to do with any REAL project unless you absolutely had to. Overuse of monkey patching can make things messy and confusing. At any rate, I hope you enjoyed this read. Have a great week.

19 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I know this thing. it's called direct assignment of class attribs. You call it as "monkey patching". It is fun, this name. Some patching...

    ReplyDelete
  3. This usage of __dict__ is never needed in real app, ie app which not uses hacks of 3rd-4rd-5th party library, always usual attribs used in real apps: myclass.attr:= someth.

    ReplyDelete
    Replies
    1. I realize that. This was just me saying "Hey look guys, check it out! There are dictionaries everywhere!" I leave it up to the reader to determine whether or not that sort of trivia is useful for them. Although, yes, generally it isn't.

      Delete
  4. nice post..great idea ..keep update with your blogs

    python online training

    ReplyDelete

  5. Iam very impressive your site gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you..

    python online training india

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Thank you for sharing very useful information nice blog
    python training in Hyderabad the best career

    ReplyDelete
  8. Today Telugu news updates provide us the information of breaking news and live updates. we get live news, political, education, technology, etc. Today Telugu news gives the best news updates. It also keeps its readers informed about the latest happenings in the world with instant updates.

    ReplyDelete
  9. Thanks you for sharing this unique information with us. It is really awesome. keep on blogging and also if anybody want to know more about python course please visit our website
    Python Training In Bangalore

    ReplyDelete
  10. You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and very broad for me.

    야설
    휴게텔
    출장안마
    출장마사지
    바카라사이트

    ReplyDelete
  11. Your article is very helpful.You can visit my website 스포츠토토

    ReplyDelete
  12. After looking into a few of the blog posts on your site, I seriously appreciate your way of blogging. 스포츠토토


    ReplyDelete
  13. I would recommend your website to everyone. You have a very good gloss. Write more high-quality articles. I support you. 파친코

    ReplyDelete
  14. Hii
    Thank you for sharing this information, the right guidance and resources, you can certainly gain a better understanding. Let's work together to break down the complexities and make it more comprehensible for you. Here is sharing some Tableau Certification Training journey information may be its helpful to you.

    Tableau Certification Training

    ReplyDelete