How to Define a Method in Python Class?

Define Method in Python Class

Python Class can contain multiple methods. To define a method in Python Class, you can use def keyword and function syntax.

Simple Example for Creating a Method in Python Class

class Developer:
  def createProject(self):
    print('The developer created a project.')
Run this program ONLINE
  • We have created a method named createProject.
  • This method does not accept any arguments. self is the default argument that has to be provided in the definition.
  • There is only a single statement in the method. However, you can add more statements.
  • The function does not return any value.

Calling the method

You can call the method using class object.

class Developer:
  hoursperday = 8
  
  def createProject(self):
    print('The developer created a project.')
	
#create object
dev1 = Developer()

#call object's method
dev1.createProject()
Run this program ONLINE

Output

The developer created a project.
Run this program ONLINE