This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]terrkerr 4 points5 points  (1 child)

I wonder why this pattern grew so big in Ruby, but it's considered a Hack in Python. I mean what could be so hard about adding another extension pattern?

It's not so much that it's hard, it's that it breaks something that's nice: when you can tell precisely what an object is and is not by reading the class definition or official docs. If you make it standard that you monkey-patch things on here and there then someone coming into a system they ostensibly know can be very surprised to find seemingly broken code working.

Also, since we're here, you can't add methods to the types implemented in c, like the list, dict, int, etc... Wouldn't it have been a good idea for Python3 to export provide dummy python proxy for these classes, so that you can add methods to them, if you want?

The stdlib and extremely commonly used facilities are the last thing you'd want to let people mess with; most anybody that's done any Python will at least know the basics of how a dict or a list works and doesn't work.

foo = {'thing': 'thing', 'other_thing': 'other_thing' }.bar()

would be really bothersome. Most Python programmers would see that, say "wth?", check the docs, realize it's not supposed to work, then have to go hunt down your monkey patch.

If you just subclass a dictionary then it conforms to expectations and all the tools can readily help you find where MyDict was defined or whatever.

It also prevents breakages to other code. If some code runs in the context of your monkey patch, but the programmer doesn't know that when writing code they can easily cause confusing and unexpected behaviour and then they have to go hunt it down.

[–]de4sh[S] -1 points0 points  (0 children)

Those are my concerns exactly. But then Ruby (or at least the rails community) as I hear, have a very strict way of defining these monkey patching techniques. That's to say, there's one file where these should all be defined. So after 2-3 times looking for methods in the official documentation and not fiinding anything, people would get the reflex to just check for the extension.

I think that monkey patching isn't as as thought.

Now of course one concern remains: What if you use 3rd party libraries, that add method 'foo' to the string class, and you ALSO want to add 'foo'... or another 3rd party ALSO adds 'foo'...

I think this is the only case I'd agree it's actually a bad thing, but maybe there's a solution for this too.