monkeypatching

Can I add custom methods/attributes to built-in Python types?

丶灬走出姿态 提交于 2019-11-26 04:28:00
For example—say I want to add a helloWorld() method to Python's dict type. Can I do this? JavaScript has a prototype object that behaves this way. Maybe it's bad design and I should subclass the dict object, but then it only works on the subclasses and I want it to work on any and all future dictionaries. Here's how it would go down in JavaScript: String.prototype.hello = function() { alert("Hello, " + this + "!"); } "Jed".hello() //alerts "Hello, Jed!" Here's a useful link with more examples— http://www.javascriptkit.com/javatutors/proto3.shtml You can't directly add the method to the

Patch routine call in delphi

房东的猫 提交于 2019-11-26 03:56:37
问题 I want to patch a routine call to be able to handle it myself with some modifications. I am writing a resource loader. I want to patch the Delphi\'s LoadResourceModule and InitInheritedComponent routines with that of mine. I have checked PatchAPI call in MadExcept.pas unit, but couldn\'t figure it out if i can use that for my project. I want something like my exe at runtime calls -> LoadResourceModule -> jump to -> MyCustomResourceModule... Any pointers on this would be very helpful. 回答1: I

Can you monkey patch methods on core types in Python?

丶灬走出姿态 提交于 2019-11-26 00:58:54
问题 Ruby can add methods to the Number class and other core types to get effects like this: 1.should_equal(1) But it seems like Python cannot do this. Is this true? And if so, why? Does it have something to do with the fact that type can\'t be modified? Update: Rather than talking about different definitions of monkey patching, I would like to just focus on the example above. I have already concluded that it cannot be done as a few of you have answered. But I would like a more detailed

Can I add custom methods/attributes to built-in Python types?

折月煮酒 提交于 2019-11-26 00:55:13
问题 For example—say I want to add a helloWorld() method to Python\'s dict type. Can I do this? JavaScript has a prototype object that behaves this way. Maybe it\'s bad design and I should subclass the dict object, but then it only works on the subclasses and I want it to work on any and all future dictionaries. Here\'s how it would go down in JavaScript: String.prototype.hello = function() { alert(\"Hello, \" + this + \"!\"); } \"Jed\".hello() //alerts \"Hello, Jed!\" Here\'s a useful link with

Making object JSON serializable with regular encoder

我是研究僧i 提交于 2019-11-26 00:43:56
问题 The regular way of JSON-serializing custom non-serializable objects is to subclass json.JSONEncoder and then pass a custom encoder to dumps. It usually looks like this: class CustomEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, foo): return obj.to_json() return json.JSONEncoder.default(self, obj) print json.dumps(obj, cls = CustomEncoder) What I\'m trying to do, is to make something serializable with the default encoder. I looked around but couldn\'t find anything. My

How to add property to a class dynamically?

梦想的初衷 提交于 2019-11-26 00:19:06
问题 The goal is to create a mock class which behaves like a db resultset. So for example, if a database query returns, using a dict expression, {\'ab\':100, \'cd\':200} , then I would like to see: >>> dummy.ab 100 At first I thought maybe I could do it this way: ks = [\'ab\', \'cd\'] vs = [12, 34] class C(dict): def __init__(self, ks, vs): for i, k in enumerate(ks): self[k] = vs[i] setattr(self, k, property(lambda x: vs[i], self.fn_readyonly)) def fn_readonly(self, v) raise \"It is ready only\"

When monkey patching an instance method, can you call the overridden method from the new implementation?

大憨熊 提交于 2019-11-25 22:59:59
问题 Say I am monkey patching a method in a class, how could I call the overridden method from the overriding method? I.e. Something a bit like super E.g. class Foo def bar() \"Hello\" end end class Foo def bar() super() + \" World\" end end >> Foo.new.bar == \"Hello World\" 回答1: EDIT : It has been 9 years since I originally wrote this answer, and it deserves some cosmetic surgery to keep it current. You can see the last version before the edit here. You can’t call the overwritten method by name

Adding a Method to an Existing Object Instance

浪子不回头ぞ 提交于 2019-11-25 21:43:33
问题 I\'ve read that it is possible to add a method to an existing object (i.e., not in the class definition) in Python. I understand that it\'s not always good to do so. But how might one do this? 回答1: In Python, there is a difference between functions and bound methods. >>> def foo(): ... print "foo" ... >>> class A: ... def bar( self ): ... print "bar" ... >>> a = A() >>> foo <function foo at 0x00A98D70> >>> a.bar <bound method A.bar of <__main__.A instance at 0x00A9BC88>> >>> Bound methods

Can you monkey patch methods on core types in Python?

梦想与她 提交于 2019-11-25 21:37:17
Ruby can add methods to the Number class and other core types to get effects like this: 1.should_equal(1) But it seems like Python cannot do this. Is this true? And if so, why? Does it have something to do with the fact that type can't be modified? Update: Rather than talking about different definitions of monkey patching, I would like to just focus on the example above. I have already concluded that it cannot be done as a few of you have answered. But I would like a more detailed explanation of why it cannot be done, and maybe what feature, if available in Python, would allow this. To answer