Adding methods to class python

Adding Functions to Python Classes

Classes can be created as simply a collection of functions. The functions can be defined within the class exactly the same way that functions are normally created. In order to call these functions we need to call it from the class. So the example class called Greetings below takes a name and returns a greeting to the person calling the class.

class Greetings: def good_morning(name): print(f'Good morning ') def good_afternoon(name): print(f'Good afternoon ') def good_evening(name): print(f'Good evening ') Greetings.good_afternoon('John') Greetings.good_morning('Peter') Greetings.good_evening('Jane') 
Out: Good afternoon John Good morning Peter Good evening Jane

Common Mistake

Although the class above behaves as we would expect. Let’s take the exact same class and add an __init__. See this article if you don’t know what the __init__ is.

class Greetings: def __init__(self): pass def good_morning(name): print(f'Good morning ') def good_afternoon(name): print(f'Good afternoon ') def good_evening(name): print(f'Good evening ') g = Greetings()

We have created an instance of the Greetings class named g. If we try to call the functions we defined within the class as follows:

TypeError: good_afternoon() takes 1 positional argument but 2 were given

It may not immediately be clear as to why this is happening. The TypeError returned above indicates that we have passed 2 arguments to the function, when it appears we have only passed one. Let’s try calling it without a name to see what happens:

 g.good_afternoon() # out: Good afternoon

What is going on here?

When we create an instance of a class the first argument passed to the function is the instance itself. So the reason we are getting the TypeError is that the way Python is reading the g.good_afternoon(‘John’) function is g.good_afternoon(g, ‘John’) although this may seem confusing, it will become clear in the next section why this happens.

Instance Methods

Taking a new example class called Student, which takes a first name, last name, age and major.

class Student: def __init__(self, first, last, age, major): self.first = first self.last = last self.age = age self.major = major def profile(self): print(f"Student name ") print(f"Student age: ") print(f"Major: ") s = Student('Sally' , 'Harris', 20, 'Biology') s.profile() 
Out: Student name Sally Harris Student age: 20 Major: Biology

When creating instance methods we must pass the self keyword, this takes care of the issue of the instance being passed to functions as the first argument. Let’s build on the current class and add enrollment functionality to show the ability of instance methods to interact with attributes.

class Student: def __init__(self, first, last, age, major): self.first = first self.last = last self.age = age self.major = major self.courses = [] def profile(self): print(f"Student name ") print(f"Student age: ") print(f"Major: ") def enrol(self, course): self.courses.append(course) print(f"enrolled in ") def show_courses(self): print(f" is taking the following courses") for course in self.courses: print(course) s = Student('Sally' , 'Harris', 20, 'Biology') s.enrol('Biochemistry I') # enrolled Sally in Biochemistry I s.enrol('Literature') # enrolled Sally in Literature s.enrol('Mathematics') # enrolled Sally in Mathematics s.show_courses() # SallyHarris is taking the following courses # Biochemistry I # Literature # Mathematics 

All of the methods above have been bound to the instance (we named this instance s), we can check this by using the dir keyword on the instance to see all the attributes and methods bound to s.

Out: ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'courses', 'enrol', 'first', 'last', 'major', 'profile', 'show_courses']

We can add a mix of instance bound functions and normal functions as defined at the beginning of the document. Below we add a method that prints the current academic year.

import datetime as dt class Student: def __init__(self, first, last, age, major): self.first = first self.last = last self.age = age self.major = major self.courses = [] def profile(self): print(f"Student name ") print(f"Student age: ") print(f"Major: ") def enrol(self, course): self.courses.append(course) print(f"enrolled in ") def show_courses(self): print(f" is taking the following courses") for course in self.courses: print(course) def academic_year(): now = dt.datetime.now() s = now.year, now.year -1 print(f"Current academic year is < str(s[0]) + '/' + str(s[1]) >")

However, we will still get an error if we try to call this new function from an instance, as we have established that calling methods/ functions from an instance always passes the instance itself in as the first argument. So If we wanted to call this academic year function we can do so as follows:

Student.academic_year() # returns: Current academic year is 2020/2019

A better way to handle this will be the topic of the next article on class & static methods.

Читайте также:  Java hibernate annotation type

Summary

— When calling a function from a class the syntax is ClassName.FunctionName()

— When calling a function bound to an instance of a class, the first argument passed in to the function is the instance itself. These are known as methods

— To access and call a method correctly we should pass self in as the first argument when writing the code for the method.

— These methods allow us to interact with attributes associated with the instance / class.

Источник

BRG blog

vimeo vimeo flickr BLOCK Research Group

The normal way to add functionality (methods) to a class in Python is to define functions in the class body. There are many other ways to accomplish this that can be useful in different situations.

This is the traditional way

class A(object): def print_classname(self): print self.__class__.__name__

The method can also be defined outside the scope of the class. This allows the function “print_classname” to be used as a standalone function and as a method of the class.

def print_classname(a): print a.__class__.__name__ class A(object): print_classname = print_classname
def print_classname(a): print a.__class__.__name__ class A(object): pass setattr(A, "print_classname", print_classname)

Adding the method to an object of type “A” is also possible. However, you need to specify that the attribute “print_classname” of the object is a method to make sure it will receive a reference to “self” as implicit first parameter when it is called.

from types import MethodType def print_classname(a): print a.__class__.__name__ class A(object): pass # this assigns the method to the instance a, but not to the class definition a = A() a.print_classname = MethodType(print_classname, a, A) # this assigns the method to the class definition A.print_classname = MethodType(print_classname, None, A)

Specific methods from another class can also be added (without inherit everything else) by adding the underlying function of the method. Otherwise the method will expect a reference to an instance of the original class as implicit first parameter.

class B(object): def print_classname(self): print self.__class__.__name__ # option 1 class A(object): print_classname = B.print_classname.__func__ # option 2 class A(object): pass setattr(A, "print_classname", B.print_classname.__func__)

3 Responses to “Adding methods to Python classes”

  1. JJ says: September 8, 2017 at 2:45 pm Great writeup. Is there a way to add a method to a different class in Python 3? __func__ is gone
    • tom says: November 8, 2017 at 8:01 am not sure, but i think you can’t as the concept of an *unbound* method has disappeared in Python 3 (see: https://stackoverflow.com/questions/3589311/get-defining-class-of-unbound-method-object-in-python-3/25959545#25959545)…
  2. dave coffee says: May 7, 2018 at 10:15 pm nice to hear this. you have the great site!
Читайте также:  Python requests proxy error

Источник

How To Add A Method To An Existing Object Instance In Python?

Add a method to an existing object instance in Python

If you are looking for an instruction to add a method to an existing object instance in Python, keep reading our article. Today, we will provide some methods to show you how to do it.

Add a method to an existing object instance in Python

Use the function __get__()

In the Python programming language, the function __get__() can be used to define a dynamic method, attribute, or value. When declaring the function in a class, its performance is to return the value. We can add a method to an existing object when declaring the function outer the class.

Detailed implementation in the following code:

# Create an object class class Student: def __init__(self, name, age, subject): self.name = name self.age = age self. subject = subject def getName(self): return self.name def getAge(self): return self.age # Initialize an object std1 = Student("John", 15, "Calculus") # Create a new method def getSubject(self): return self.subject # Add the method to the object std1 std1.getSubject = getSubject.__get__(std1) print(std1.getSubject())

Use types.MethodType

Types.MethodType is a method that belongs to the library types and enables users to define a new method for an existing object. This way, an existing object can use a new-defined method as the initial method in the object class.

object.newMethod = types.MethodType(newMethod, object)

import types # Create an object class class Student: def __init__(self, name, age, subject): self.name = name self.age = age self. subject = subject def getName(self): return self.name def getAge(self): return self.age # Initialize an object std1 = Student("John", 15, "Calculus") # Create a new method def getSubject(self): return self.subject # Add the method to the object std1 std1.getSubject = types.MethodType(getSubject, std1) print(std1.getSubject())

Use functools.partial

The module “functools” provides the type partial returning the object that operates as a function called. When using this way, a partial function is applied to the object and makes the object have a method similar to methods inside the class object.

from functools import partial # Create an object class class Student: def __init__(self, name, age, subject): self.name = name self.age = age self. subject = subject def getName(self): return self.name def getAge(self): return self.age # Initialize an object std1 = Student("John", 15, "Calculus") # Create a new method def getSubject(self): return self.subject # Add the method to the object std1 std1.getSubject = partial(getSubject, std1) print(std1.getSubject())

Summary

Our tutorial provides some detailed examples to add a method to an existing object instance in Python. We hope you gain much knowledge from our article. If you have any troubles, do not hesitate to give us your questions. Thanks for reading!

Читайте также:  Строим график в питоне

Maybe you are interested:

My name is Robert Collier. I graduated in IT at HUST university. My interest is learning programming languages; my strengths are Python, C, C++, and Machine Learning/Deep Learning/NLP. I will share all the knowledge I have through my articles. Hope you like them.

Name of the university: HUST
Major: IT
Programming Languages: Python, C, C++, Machine Learning/Deep Learning/NLP

Источник

Оцените статью