How to add a function to a class in python
Preface - a note on compatibility: other answers may only work in Python 2 - this answer should work perfectly well in Python 2 and 3. If writing Python 3 only, you might leave out explicitly inheriting from Show
Yes, it is possible - But not recommendedI don't recommend this. This is a bad idea. Don't do it. Here's a couple of reasons:
Thus, I suggest that you not do this unless you have a really good reason. It is far better to define the correct method in the class definition or less preferably to monkey-patch the class directly, like this:
Since it's instructive, however, I'm going to show you some ways of doing this. How it can be doneHere's some setup code. We need a class definition. It could be imported, but it really doesn't matter.
Create an instance:
Create a method to add to it:
Method nought (0) - use the descriptor method, __get__Dotted lookups on functions call the
and now:
Method one - types.MethodTypeFirst, import types, from which we'll get the method constructor:
Now we add the method to the instance. To do this, we require the MethodType constructor from the The argument signature for types.MethodType (in Python 3) is
and usage:
Parenthetically, in Python 2 the
signature was
Method two: lexical bindingFirst, we create a wrapper function that binds the method to the instance:
usage:
Method three: functools.partialA partial function applies the first argument(s) to a function (and optionally keyword arguments), and can later be called with the remaining arguments (and overriding keyword arguments). Thus:
This makes sense when you consider that bound methods are partial functions of the instance. Unbound function as an object attribute - why this doesn't work:If we try to add the sample_method in the same way as we might add it to the class, it is unbound from the instance, and doesn't take the implicit self as the first argument.
We can make the unbound function work by explicitly passing the instance (or anything, since this method doesn't actually use the
ConclusionYou now know several ways you could do this, but in all seriousness - don't do this. How do you add a function in Python?How to Create User-Defined Functions in Python. Use the def keyword to begin the function definition.. Name your function.. Supply one or more parameters. ... . Enter lines of code that make your function do whatever it does. ... . Use the return keyword at the end of the function to return the output.. How do you define a function in a class Python?We must explicitly tell Python that it is a class method using the @classmethod decorator or classmethod() function. Class methods are defined inside a class, and it is pretty similar to defining a regular function. Like, inside an instance method, we use the self keyword to access or modify the instance variables.
What is __ add __ method in Python?The __add__() method in Python specifies what happens when you call + on two objects. When you call obj1 + obj2, you are essentially calling obj1. __add__(obj2).
What is __ init __ function in classes?The __init__ method is the Python equivalent of the C++ constructor in an object-oriented approach. The __init__ function is called every time an object is created from a class. The __init__ method lets the class initialize the object's attributes and serves no other purpose. It is only used within classes.
|