How to define Method in a Class in Python?

Python – Define a method in a class

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

Example

class Developer:
  def createProject(self):
    print('The developer created a project.')
Run Code

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 Code

Output

The developer created a project.
Run Code

Summary

In this Python Classes and Objects tutorial, we learned how to define a method in a class.

Related Tutorials

Privacy Policy Terms of Use

SitemapContact Us