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

前端 未结 8 1379
一向
一向 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条回答
  •  离开以前
    2021-02-05 12:41

    In Python, any literal ("", {}, 1.0, etc) creates an instance of the standard class, even if you tried to monkeypatch it and redefined the corresponding class in your namespace.

    It just won't work how you intended:

    class str():
        # define your custom string type
        ...
    
    a = "foo"      # still a real Python string
    a = str("foo") # only this uses your custom class
    

提交回复
热议问题