Methods in Python
flowchart TD
A["def m(self) inside class C"] --> B["C.__dict__['m'] is a plain function"]
B --> C{"how is it accessed?"}
C -->|"C.m"| D["the function itself"]
D --> E["C.m(c) works -- you pass self"]
D --> F["C.m() -> TypeError: missing 'self'"]
C -->|"c.m"| G["a bound method"]
G --> H["__self__ is c"]
H --> I["c.m() supplies self automatically"]
J["@classmethod"] --> K["bound to the CLASS -- receives cls"]
L["@staticmethod"] --> M["no binding at all -- a plain function in a namespace"]
Methods in Class in Python: A Comprehensive Guide
Section titled “Methods in Class in Python: A Comprehensive Guide”In Python, methods are functions defined within a class. They encapsulate behavior that is specific to instances of the class. Understanding methods is essential for effective object-oriented programming. Let’s delve into the properties, advantages, and potential disadvantages of methods in Python classes.
Properties of Methods in Python Classes:
Section titled “Properties of Methods in Python Classes:”-
Encapsulation:
- Description: Methods encapsulate behavior, grouping related functionality within a class.
- Advantage: Encapsulation promotes code organization, making it easier to manage and maintain.
-
Self Parameter:
- Description: All methods in a class have
selfas their first parameter, representing the instance calling the method. - Advantage: Allows methods to access and manipulate the state of the instance, ensuring proper encapsulation.
- Description: All methods in a class have
-
Access to Class Attributes:
- Description: Methods have access to the class’s attributes and can modify them using the
selfparameter. - Advantage: Facilitates the manipulation of object state, contributing to the concept of encapsulation.
- Description: Methods have access to the class’s attributes and can modify them using the
-
Behavior Definition:
- Description: Methods define the behavior of objects instantiated from the class.
- Advantage: Enables the modeling of real-world actions, providing a blueprint for how instances should operate.
-
Code Reusability:
- Description: Methods can be reused across different instances of the same class.
- Advantage: Promotes code reusability, reducing redundancy and improving maintainability.
Advantages of Methods in Python Classes:
Section titled “Advantages of Methods in Python Classes:”-
Modularity:
- Description: Methods contribute to the modularity of code by organizing functionality into discrete units.
- Advantage: Enhances code readability, maintenance, and the ability to make targeted updates.
-
Object-Specific Behavior:
- Description: Methods allow for the definition of behavior specific to each instance of a class.
- Advantage: Enables objects to exhibit unique actions based on their state.
-
Inheritance:
- Description: Methods support the inheritance mechanism, allowing subclasses to override or extend methods from their superclass.
- Advantage: Facilitates code reuse and the creation of specialized classes.
-
Code Clarity:
- Description: Methods contribute to a clearer organization of code by associating related actions with a specific class.
- Advantage: Improves code maintainability and makes it easier for developers to understand the functionality of a class.
-
Encapsulation of State:
- Description: Methods work in conjunction with attributes to encapsulate the state of an object.
- Advantage: Promotes data integrity and makes it easier to manage and control access to object state.
Potential Disadvantages of Methods in Python Classes:
Section titled “Potential Disadvantages of Methods in Python Classes:”-
Complexity:
- Description: As the number of methods in a class increases, the complexity of the class may also rise.
- Disadvantage: A highly complex class can be challenging to understand and maintain.
-
Tight Coupling:
- Description: Methods may introduce dependencies between different parts of a class, leading to tight coupling.
- Disadvantage: High coupling can make the class less flexible and harder to modify without affecting other components.
-
Overhead:
- Description: Adding numerous methods to a class might introduce a slight runtime overhead.
- Disadvantage: While typically negligible, excessive method calls could impact performance in some scenarios.
-
Overuse of Accessors and Mutators:
- Description: Excessive use of accessor and mutator methods (getters and setters) might violate principles of good design.
- Disadvantage: It can lead to classes that are overly focused on exposing internal state, potentially undermining encapsulation.
-
Learning Curve:
- Description: For individuals new to object-oriented programming, understanding the concept of methods and their proper use can pose a learning curve.
- Disadvantage: Novice programmers may initially find it challenging to grasp when and how to use methods effectively.
Types of Methods in Python Classes:
Section titled “Types of Methods in Python Classes:”- Instance Methods
- Class Methods
- Static Methods
- Getter and Setter Methods
Instance Methods
Section titled “Instance Methods”Instance methods are the most common type of methods in Python classes. They are defined within a class and are accessible only through an instance of the class. Instance methods have access to the instance’s state through the self parameter. They can also access the class’s attributes and other methods using the self parameter.
Syntax of Instance Methods in Python Classes:
Section titled “Syntax of Instance Methods in Python Classes:”class ClassName:
def method_name(self, parameters):
# Method bodyExample of Instance Methods in Python Classes:
Section titled “Example of Instance Methods in Python Classes:”class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def display(self):
print('Name:', self.name)
print('Salary:', self.salary)
employee1 = Employee('John', 10000)
employee2 = Employee('Bob', 20000)
employee1.display()
employee2.display()Output:
C:\Users\username>python instance_method.py
Name: John
Salary: 10000
Name: Bob
Salary: 20000In the above example, we have created two instance variables named name and salary. We have initialized the name and salary variables to the name and salary parameters of the __init__() method. We have printed the name and salary variables using the employee1 and employee2 objects. The output shows that the name and salary variables are unique to the object.
Another Example of Instance Methods in Python Classes:
class Student:
def register(self, name, roll):
self.name = name
self.roll = roll
def display(self):
print('Name:', self.name)
print('Roll:', self.roll)
student1 = Student()
student1.register('John', 1)
student1.display()Output:
C:\Users\username>python instance_method.py
Name: John
Roll: 1In the above example, we have created two instance variables named name and roll. We have initialized the name and roll variables to the name and roll parameters of the register() method. We have printed the name and roll variables using the student1 object. The output shows that the name and roll variables are unique to the object.
Class Methods
Section titled “Class Methods”Class methods are methods that are bound to a class rather than its instances. They are defined using the @classmethod decorator and have access to the class’s state through the cls parameter. Class methods can be used to create factory methods, which are methods that return an instance of the class. They can also be used to modify a class’s state that applies across all instances of the class. Class methods are commonly used as alternative constructors.
Syntax of Class Methods in Python Classes:
Section titled “Syntax of Class Methods in Python Classes:”class ClassName:
@classmethod
def method_name(cls, parameters):
# Method bodyExample of Class Methods in Python Classes:
Section titled “Example of Class Methods in Python Classes:”class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def display(self):
print('Name:', self.name)
print('Salary:', self.salary)
@classmethod
def from_string(cls, emp_str):
name, salary = emp_str.split('-')
return cls(name, salary)
employee1 = Employee('John', 10000)
employee2 = Employee.from_string('Bob-20000')
employee1.display()
employee2.display()Output:
C:\Users\username>python class_method.py
Name: John
Salary: 10000
Name: Bob
Salary: 20000In the above example, we have created two instance variables named name and salary. We have initialized the name and salary variables to the name and salary parameters of the __init__() method. We have printed the name and salary variables using the employee1 and employee2 objects. The output shows that the name and salary variables are unique to the object. We have created a class method named from_string() using the @classmethod decorator. We have called the from_string() method using the Employee class and employee2 object. The output shows that the from_string() method is accessible through both the Employee class and employee2 object.
Another Example of Class Methods in Python Classes:
class IronMan:
def suitUp(self):
print('Suiting up...')
def startEngine(self):
print('Starting engine...')
def workingJarvis(self):
print('Working Jarvis...')
def fly(self):
print('Flying...')
def land(self):
print('Landing...')
def suitDown(self):
print('Suiting down...')
def journey(self, location):
print('Journey started to ' + location)
@classmethod
def goToMars(cls):
cls().suitUp()
cls().startEngine()
cls().workingJarvis()
cls().fly()
cls().journey('Mars')
cls().land()
cls().suitDown()
IronMan.goToMars()
mark1 = IronMan()
mark1.goToMars()Output:
C:\Users\username>python class_method.py
Suiting up...
Starting engine...
Working Jarvis...
Flying...
Journey started to Mars
Landing...
Suiting down...In the above example, we have created seven instance methods named suitUp(), startEngine(), workingJarvis(), fly(), land(), suitDown(), and journey(). We have created a class method named goToMars() using the @classmethod decorator. We have called the goToMars() method using the IronMan class and mark1 object. The output shows that the goToMars() method is accessible through both the IronMan class and mark1 object.
Static Methods
Section titled “Static Methods”Static methods are methods that are bound to a class rather than its instances. They are defined using the @staticmethod decorator and do not have access to the class’s state. Static methods are commonly used to create utility functions that do not require access to the class’s state. They can also be used to group related functionality within a class. Static methods are similar to class methods, but they do not receive the class as an implicit first argument.
Syntax of Static Methods in Python Classes:
Section titled “Syntax of Static Methods in Python Classes:”class ClassName:
@staticmethod
def method_name(parameters):
# Method bodyExample of Static Methods in Python Classes:
Section titled “Example of Static Methods in Python Classes:”class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def display(self):
print('Name:', self.name)
print('Salary:', self.salary)
@staticmethod
def is_valid_salary(salary):
if salary > 0:
return True
else:
return False
employee1 = Employee('John', 10000)
employee2 = Employee('Bob', 20000)
print(Employee.is_valid_salary(10000))
print(Employee.is_valid_salary(-10000))Output:
C:\Users\username>python static_method.py
True
FalseIn the above example, we have created two instance variables named name and salary. We have initialized the name and salary variables to the name and salary parameters of the __init__() method. We have printed the name and salary variables using the employee1 and employee2 objects. The output shows that the name and salary variables are unique to the object. We have created a static method named is_valid_salary() using the @staticmethod decorator. We have called the is_valid_salary() method using the Employee class. The output shows that the is_valid_salary() method is accessible through the Employee class.
Another Example of Static Methods in Python Classes:
class Calculator:
@staticmethod
def add(a, b):
return a + b
@staticmethod
def subtract(a, b):
return a - b
@staticmethod
def multiply(a, b):
return a * b
@staticmethod
def divide(a, b):
return a / b
print(Calculator.add(10, 20))
print(Calculator.subtract(10, 20))
print(Calculator.multiply(10, 20))
print(Calculator.divide(10, 20))Output:
C:\Users\username>python static_method.py
30
-10
200
0.5In the above example, we have created four static methods named add(), subtract(), multiply(), and divide(). We have called the add(), subtract(), multiply(), and divide() methods using the Calculator class. The output shows that the add(), subtract(), multiply(), and divide() methods are accessible through the Calculator class.
Getter and Setter Methods
Section titled “Getter and Setter Methods”Getter and setter methods are methods that are used to access and modify the state of an object. They are commonly used to control access to an object’s attributes. Getter methods are used to access an object’s attributes, while setter methods are used to modify an object’s attributes. Getter and setter methods are also known as accessor and mutator methods.
Syntax of Getter and Setter Methods in Python Classes:
Section titled “Syntax of Getter and Setter Methods in Python Classes:”class ClassName:
def get_attribute(self):
# Method body
def set_attribute(self, value):
# Method bodyExample of Getter and Setter Methods in Python Classes:
Section titled “Example of Getter and Setter Methods in Python Classes:”class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def display(self):
print('Name:', self.name)
print('Salary:', self.salary)
def get_name(self):
return self.name
def set_name(self, name):
self.name = name
def get_salary(self):
return self.salary
def set_salary(self, salary):
self.salary = salary
employee1 = Employee('John', 10000)
employee1.display()
employee1.set_name('Bob')
employee1.set_salary(20000)
employee1.display()
print("Employee Name:", employee1.get_name())
print("Employee Salary:", employee1.get_salary())Output:
C:\Users\username>python getter_setter_method.py
Name: John
Salary: 10000
Name: Bob
Salary: 20000
Employee Name: Bob
Employee Salary: 20000In the above example, we have created two instance variables named name and salary. We have initialized the name and salary variables to the name and salary parameters of the __init__() method. We have printed the name and salary variables using the employee1 object. The output shows that the name and salary variables are unique to the object. We have created getter and setter methods for the name and salary variables. We have called the getter and setter methods using the employee1 object. The output shows that the getter and setter methods are accessible through the employee1 object. We have printed the name and salary variables using the getter methods. The output shows that the getter methods are accessible through the employee1 object.
Another Example of Getter and Setter Methods in Python Classes:
class Student:
def __init__(self, name, roll):
self.name = name
self.age = age
def display(self):
print('Name:', self.name)
print('Age:', self.age)
def get_name(self):
return self.name
def set_name(self, name):
self.name = name
def get_age(self):
return self.age
def set_age(self, age):
if age > 18:
self.age = age
else:
print('Age must be greater than 18')
student1 = Student('John', 20)
student1.display()
student1.set_name('Bob')
student1.set_age(15)
student1.set_age(30)
student1.display()
print("Student Name:", student1.get_name())
print("Student Age:", student1.get_age())Output:
C:\Users\username>python getter_setter_method.py
Name: John
Age: 20
Age must be greater than 18
Name: Bob
Age: 30
Student Name: Bob
Student Age: 30In the above example, we have created two instance variables named name and age. We have initialized the name and age variables to the name and age parameters of the __init__() method. We have printed the name and age variables using the student1 object. The output shows that the name and age variables are unique to the object. We have created getter and setter methods for the name and age variables. We have called the getter and setter methods using the student1 object. The output shows that the getter and setter methods are accessible through the student1 object. We have printed the name and age variables using the getter methods. The output shows that the getter methods are accessible through the student1 object. In the above example, we have used the setter method to validate the age variable that it must be greater than 18. If the age variable is less than 18, then it will print the message Age must be greater than 18. Otherwise, it will set the age variable to the age parameter of the set_age() method.
Conclusion
Section titled “Conclusion”In this tutorial, you have learned about methods in Python classes. Methods are functions defined within a class. They encapsulate behavior that is specific to instances of the class. Understanding methods is essential for effective object-oriented programming. Let’s delve into the properties, advantages, and potential disadvantages of methods in Python classes. You have also learned about different types of methods in Python classes Like Instance Methods, Class Methods, Static Methods, Getter and Setter Methods. You have also learned about the properties, advantages, and potential disadvantages of methods in Python classes. For more information, visit the Python Classes page. For more tutorials like this, visit the Python Central Hub.
Check yourself
Section titled “Check yourself”-
What is `C.m` when `m` is defined with `def m(self)` in class `C`?
Verified: `C.m` prints as `<function C.m ...>`. The binding happens on attribute access through an INSTANCE, not on the class.
pch.quizShowAnswer
B — A plain function — Verified: `C.m` prints as `<function C.m ...>`. The binding happens on attribute access through an INSTANCE, not on the class.
-
`C.m()` with no arguments does what?
Reaching the function through the class gives you the bare function, so you must supply `self` — `C.m(c)` works fine.
pch.quizShowAnswer
B — TypeError: missing 1 required positional argument: 'self' — Reaching the function through the class gives you the bare function, so you must supply `self` — `C.m(c)` works fine.
-
Is `c.m is c.m` True?
Verified as False. So a bound method cannot be removed from a list of callbacks by identity, and is not stable as a dictionary key. Compare with `==`, or hold one reference.
pch.quizShowAnswer
B — No — a new bound method object is built on every access — Verified as False. So a bound method cannot be removed from a list of callbacks by identity, and is not stable as a dictionary key. Compare with `==`, or hold one reference.
-
Why does storing `obj.method` as a long-lived callback keep `obj` alive?
That reference is exactly what makes `self` available later. `weakref.WeakMethod` exists for when you want the callback not to keep the object alive.
pch.quizShowAnswer
B — The bound method holds a strong reference through `__self__` — That reference is exactly what makes `self` available later. `weakref.WeakMethod` exists for when you want the callback not to keep the object alive.
Try it: OOP Methods Exercises
Section titled “Try it: OOP Methods Exercises”Exercise 1 – Instance Method
Section titled “Exercise 1 – Instance Method”Exercise 2 – Class Method
Section titled “Exercise 2 – Class Method”Exercise 3 – Static Method
Section titled “Exercise 3 – Static Method”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading