What Does self Do in Python?
In Python, self is a special keyword that refers to the current instance of a class. It is used to access variables and methods from the class itself, rather than from an instance of the class. In this article, we will explore what self does in Python and how it is used in various scenarios.
What is self?
self is a built-in keyword in Python that represents the current instance of a class. It is used to access variables and methods from the class itself, rather than from an instance of the class. When a class is defined, Python creates an instance of the class, and self is used to access the instance’s attributes and methods.
Why Use self?
Using self provides several benefits, including:
- Accessing instance variables:
selfallows you to access the instance’s variables, which are stored in the instance’s attributes. - Accessing instance methods:
selfallows you to access the instance’s methods, which are stored in the instance’s methods. - Creating instance methods:
selfis used to create instance methods, which are methods that are bound to the instance of the class.
Types of self
There are two types of self in Python:
- Instance
self: This is the defaultselfthat is used when a class is defined without any arguments. - Class
self: This is theselfthat is used when a class is defined with arguments.
Accessing Instance Variables
Instance variables are variables that are stored in the instance of the class. To access an instance variable, you need to use the self keyword followed by the variable name.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
person = Person("John", 30)
print(person.name) # Output: John
print(person.age) # Output: 30
In the above example, name and age are instance variables that are stored in the person instance.
Accessing Instance Methods
Instance methods are methods that are bound to the instance of the class. To access an instance method, you need to use the self keyword followed by the method name.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print(f"Hello, my name is {self.name} and I am {self.age} years old.")
person = Person("John", 30)
person.greet() # Output: Hello, my name is John and I am 30 years old.
In the above example, greet is an instance method that is bound to the person instance.
Creating Instance Methods
Instance methods can be created using the self keyword followed by the method name and parentheses containing the method implementation.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print(f"Hello, my name is {self.name} and I am {self.age} years old.")
def say_hello(self):
print(f"Hello, my name is {self.name} and I am {self.age} years old.")
person = Person("John", 30)
person.greet() # Output: Hello, my name is John and I am 30 years old.
person.say_hello() # Output: Hello, my name is John and I am 30 years old.
In the above example, greet and say_hello are instance methods that are created using the self keyword.
Table: Instance Variables and Methods
| Instance Variable | Method |
|---|---|
name |
greet |
age |
greet |
name |
say_hello |
age |
say_hello |
Best Practices
Here are some best practices to keep in mind when using self in Python:
- Use
selfconsistently: Useselfconsistently throughout your code to avoid confusion. - Use
selfto access instance variables: Useselfto access instance variables to avoid accessing global variables. - Use
selfto access instance methods: Useselfto access instance methods to avoid accessing global functions. - Use
selfto create instance methods: Useselfto create instance methods to avoid creating global functions.
Conclusion
In conclusion, self is a powerful keyword in Python that allows you to access instance variables and methods from the class itself. By using self consistently and following best practices, you can write more efficient and effective code. Remember to use self to access instance variables, methods, and create instance methods to avoid confusion and create maintainable code.
