0

Take a simple example in :

>>> class A(object):
...     pass
...
>>> def f(self):
...     print "f called"
...
>>> A.f = f
>>> a = A()
>>> a.f()
f called

So in this example, the already existing class A gets an additional (instance) function f (though overriding existing ones works just as well). In real life this would of course happen e.g. in different modules. But how is this procedure called?

Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221

1 Answers1

1

Not sure I am following you, but if I do, you are talking about extension methods

amit
  • 175,853
  • 27
  • 231
  • 333
  • Basically yes, though that sounds very C# specific. The question [linked to by Daniel Williams](http://stackoverflow.com/questions/18141875/what-is-adding-methods-to-a-class-instead-of-properly-subclassing-called#comment26569980_18141875) uses the tag [tag:monkeypatching], which seems to describe it slightly better – Tobias Kienzler Aug 09 '13 at 08:11
  • Extension methods is a term, and not a C# specific, it is implemented in C# very nicely, and c# happens to be a very common PL, so the examples for extension methods are taken from C# usually. (It is supposed to be introduced to java8 as well, if they didn't change the plan) – amit Aug 09 '13 at 08:14
  • @TobiasKienzler moneypatching seems to be more specific for dynamic languages, such as python, so if you are referring to dynamic languages only, and not the general concept, I think you might be right, and it is a better term. – amit Aug 09 '13 at 08:24
  • Ah, that makes sense. So I'll accept your answer but refer to this as monkeypatching when using Python :-7 – Tobias Kienzler Aug 09 '13 at 08:31
  • Ok, [as Jeff states](http://www.codinghorror.com/blog/2008/07/monkeypatching-for-humans.html), monkeypatching is basically extension methods taken that one step further to _modify_ already existing methods – Tobias Kienzler Aug 09 '13 at 09:50