Class Methods in Python
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Class Methods in Python

Kishore V

Class Methods in Python

Methods are functions defined inside a class. They describe the actions or behavior of objects created from that class.

Each method automatically receives the current object through the self parameter.

Defining a Method in a Class

Methods are created just like functions, but they are placed inside a class definition.

Example: Simple Class Method

Hi, I am Ananya

Note: self must always be the first parameter of an instance method.

Methods with Parameters

Methods can accept additional parameters, exactly like normal functions outside of a class.

Example: Method with Arguments

6 32

Methods Accessing Object Properties

Methods can easily read an object's internal data using the self reference.

Example: Access Properties Inside a Method

Riya lives in India

Methods Modifying Object Properties

Methods can also update the values of object properties, permanently changing the state of the object.

Example: Modify a Property

Current value: 1 Current value: 2

The __str__() Method

The __str__() method defines what should be displayed when an object is printed as a string.

Example: Without __str__()

<__main__.Device object at 0x...>

Example: With __str__()

Device Name: Router

Using Multiple Methods in a Class

A class can contain multiple methods that work together to perform tasks.

Example: Multiple Methods Working Together

Task added: Study Python Task added: Practice OOP Tasks: - Study Python - Practice OOP

Deleting Methods from a Class

You can remove a method from a class using the del keyword. This affects all objects of that class. This is rarely used in real-world programs.

Example: Delete a Class Method

# Method successfully deleted from the class memory
Our website uses cookies to enhance your experience. Learn More
Accept !