Does Python have extension methods like C#? Is it possible to call a method like:
MyRandomMethod()
on existing types like int
?
myInt.MyRandomMethod()
You can add whatever methods you like on class objects defined in Python code (AKA monkey patching):
>>> class A(object):
>>> pass
>>> def stuff(self):
>>> print self
>>> A.test = stuff
>>> A().test()
This does not work on builtin types, because their __dict__
is not writable (it's a dictproxy
).
So no, there is no "real" extension method mechanism in Python.