If monkey patching is permitted in both Ruby and Python, why is it more controversial in Ruby?

前端 未结 8 1397
一向
一向 2021-02-05 12:34

In many discussions I have heard about Ruby in which people have expressed their reservations about the language, the issue of monkey patching comes up as one of their primary c

8条回答
  •  旧时难觅i
    2021-02-05 12:48

    If you want to do some monkey patching in Python, it is relatively easy, as long as you are not modifying a built-in type (int, float, str).

    class SomeClass:
        def foo(self):
            print "foo"
    
    def tempfunc(self):
        print "bar"
    SomeClass.bar = tempfunc
    del tempfunc
    

    This will add the bar method to SomeClass and even existing instances of that class can use that injected method.

提交回复
热议问题