Page Contents
In this tutorial we will learn about Object method in Python class . Before reading further we recommend to read about the below tutorial –
Object method in Python
The object method is the behavior of the object, which can only be accessed through an object.
Syntax of object method in Python
def method_name(self,parameters): statements
Parameters of object method in Python
The first parameter in the object method is always self. Then we need to pass the required parameters. self is a reference to a given object. We can use anything in place of self but whatever it is the first parameter is always treated as a reference to the given object. But only self is used as convention.
Since self is used as a convention, all programmers use self as the first parameter in the object method. So if we read someone else’s program or someone else reads our program and there is something written in the first parameter of the object method other than self then we may get confused. So we will always use self in the first parameter of the object method.
Calling a object method in Python
object_name.method_name(parameters)
Examples of Object method in Python
Example 1:
# creating a class named Car
class Car:
# here we use obj in the position of self
# defining object method start_car
def start_car(obj):
print("car has started")
obj.speed = 0
# defining object method set_speed
def set_speed (obj, speed):
print("Your car is running now")
obj.speed = speed
In the above code, a class named Car
has been written with two methods named start_car
and set_speed
. When defining start_car
takes one parameter and set_speed
takes two parameters. In the start_car
method a variable named speed
has been created whose value is 0
and in the set_speed
method the value of the speed
variable has been changed. Since the speed
variable is defined in the object method, speed
is an object variable that can only be accessed with an object.
# creating a object of class Car
BMW = Car()
# calling first method
BMW.start_car()
# we can also call this as
Car.start_car(BMW)
car has started car has started
Now an object named BMW
has been created and its start_car
method has been called. Notice that we did not use any argument in the start_car
method where we used a parameter while defining but the program did not show any error. Here, in fact, the object i.e. BMW
has been treated as an argument. Which we can see in the next line when we run the same method by typing Car.start_car(BMW)
which gives the same output. So object itself is treated as the first argument of any object method .
# from start_car method a object variable
# speed is created
print(BMW.speed)
0
After calling the start_car
method an object variable speed
is created whose value is 0
, so when the speed
is printed it prints 0
.
# calling second method
BMW.set_speed(40)
# we can also call this as
Car.set_speed(BMW, 40)
Your car is running now Your car is running now
The set_speed
method is then called and an argument is given while calling but two parameters are used during definning .As first argument is the object itself so it didn`t raise any error . So when Car.set_speed(BMW, 40)
is called, it prints the same in the output.
# we can see value of object variable
# speed is also changed
print(BMW.speed)
40
When calling set_speed
method, we have used 40
as the speed , so the value of speed
variable has changed to 40
. So when speed
is printed it prints 40
.
So since the first parameter used during the object method definition (here obg
) is a reference to the object, it can be seen that the object variable in the object method has also been accessed with the help of the object (e.g. obj.speed
).
Next time we will always use self
in the first parameter of the object method, here we have tried to use obj
to show that it is not necessary to use self
.
Example 2:
#creating a class named Car
class Car:
def set_speed():
print("Your car is running now")
#creating a object of class Car
BMW = Car()
# calling set_speed method
BMW.set_speed()
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_12788/3158896883.py in <module> 7 BMW = Car() 8 # calling set_speed method ----> 9 BMW.set_speed() TypeError: set_speed() takes 0 positional arguments but 1 was given
In the above code we have written a class named Car
where there is an object method called set_speed
. Here we have not given any parameter while set_speed
method definition. Then when we create an object and try to call the object method without argument, it shows error and in the error it says set_speed() takes 0 positional arguments but 1 was given
. But during method calling we did not give any argument. This means that the object itself has been passed as an argument in the set_speed
method.
Note :
We know that almost everything in Python is an object. Now we will use an object that we have used many times before without knowing it as an object. Using that class we have created object, also used object method.
#creating a object of class list
list1 = list((1, 2, 3))
#apply list method
print(list1.count(1))
# we can also call this as
print(list.count(list1, 1))
1 1
In the above code we have created an object list1
of list
class. Then we call an object method count
.
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .