
The self parameter is a reference to the current instance of the class.
It is used to access properties and methods that belong to the class.
Use self to access class properties:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hello, my name is " + self.name)
p1 = Person("Emil", 25)
p1.greet()Note: The self parameter must be the first parameter of any method in the class.
Without self, Python would not know which object's properties you want to access:
The self parameter links the method to the specific object:
class Person:
def __init__(self, name):
self.name = name
def printname(self):
print(self.name)
p1 = Person("Tobias")
p2 = Person("Linus")
p1.printname()
p2.printname()It does not have to be named self, you can call it whatever you like, but it has to be the first parameter of any method in the class:
Use the words myobject and abc instead of self:
class Person:
def __init__(myobject, name, age):
myobject.name = name
myobject.age = age
def greet(abc):
print("Hello, my name is " + abc.name)
p1 = Person("Emil", 36)
p1.greet()You can access any property of the class using self:
class Car:
def __init__(self, brand, model, year):
self.brand = brand
self.model = model
self.year = year
def display_info(self):
print(f"{self.year} {self.brand} {self.model}")
car1 = Car("Toyota", "Corolla", 2020)
car1.display_info()You can also call other methods within the class using self:
class Person:
def __init__(self, name):
self.name = name
def greet(self):
return "Hello, " + self.name
def welcome(self):
message = self.greet()
print(message + " ! Welcome to our website.")
p1 = Person("Tobias")
p1.welcome()Properties are variables that belong to a class. They store data for each object created from the class.
Create a class with properties:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Emil", 36)
print(p1.name)
print(p1.age)You can access object properties using dot notation:
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
car1 = Car("Toyota", "Corolla")
print(car1.brand)
print(car1.model)Properties defined inside __init__() belong to each object (instance properties).
Properties defined outside methods belong to the class itself (class properties) and are shared by all objects:
class Person:
species = "Human" # Class property
def __init__(self, name):
self.name = name # Instance property
p1 = Person("Emil")
p2 = Person("Tobias")
print(p1.name)
print(p2.name)
print(p1.species)
print(p2.species)You can modify the value of properties on objects:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Tobias", 25)
print(p1.age)
p1.age = 26
print(p1.age)You can delete properties from objects using the del keyword:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Linus", 30)
del p1.age
print(p1.name) # This works
# print(p1.age) # This would cause an errorYou can add new properties to existing objects:
class Person:
def __init__(self, name):
self.name = name
p1 = Person("Tobias")
p1.age = 25
p1.city = "Oslo"
print(p1.name)
print(p1.age)
print(p1.city)Note: Adding properties this way only adds them to that specific object, not to all objects of the class.