How to Create Method in Python 3.8?

Class and Method In Python

Create method in python for your application. Methods are used in Object-Oriented-Programming. In last tutorial we have learn how to create encapsulation in python.  Today we discuss what is method, how to create method and create method in python with examples.

Before starting how to create method in python first we discuss a little about what is object-oriented-programming.


In Object-Oriented-Programming programmer create your own library which includes classes, module, methods and objects. Object-oriented-programming save the programmer and developer when he especially developing a multi user application for long time.

How to object-oriented-programming save programmer time?

Suppose a programming developing a school application in python so he design the following three module

  • School Enrollment Module
  • Accounts Module (collection of fee, fine, hostel fee, bus fee etc)
  • Hostel Module

So here a good programmer creates classes, method and object to organize the app. He creates all the desired methods and its functions in classes. And call the desired function in the python files.

In object-oriented-programming once a method is created you can access it in every file in your projects. And here there is no need to create a same function for other files.

 But in the procedural programming languages a developer create the same function again and again in every file.

What is method?

In python a method is a function of arguments and objects. A method I created inside the class. And you can access it outside the class if a method is public. Here we discuss two types of method which are following.

  • Public Method: Is that method which can be access outside the class.
  • Private Method: a method which not accessible outside the class.

Create Method in Python?

A method is write in python with a start keyword “def “ after this you need to write  the name of method with an argument.

Syntax :

#create method in python
#python 3.8
#declare function
def __init__(self, name, age, fee):


Types of Method:

  1. Public Method:

Create method in python you should know about the argument which place on instant. public member are accessible from outside the class. The object of the same class required to invoke a public method. Suppose a Motorbike company. Public method use to show the functionality outside the class.

By default all the argument or member in the class are public it depend on programmer which argument he make private.

Example of public method

# python 3.8
# student class
class student:

def __init__(self, name, age, fee): = name
self.age = age
self.fee = fee

std = student("sadaqat", 22, 50)
print('Student Name :',, 'Age :', std.age, 'Fee :', std.fee)

Student Name : sadaqat Age : 22 Fee : 50

  2. Private Method:

Private method is that method which can’t accessible without the class. Once a private method is written inside the class it can’t be access outside the class. Python doesn’t have any mechanism that effetely restrict to any member in the class. You make a variable or method is private you will use prefix single underscore (_) or double underscore (__) to make a variable private

Create method in python suppose a real life example, a Motorbike engine which is make up of many parts like spark plug, valves, piston, head etc. no user can use these parts directly, rather they just know how to use the parts which uses them.

Example Of Private Method:

class student:

# init method or constructor, augument
def __init__(self, name): = 'sadaqat'
self.age = 22
self.__fee = 500

#privte method
def __private_method(self):

When I am trying to access the private method outside the class, the private method not listed in the suggestion list because python know that method is private.

Now we enter the direct private method to get the access. Enter the following code outside the class.


Now run the code and you will see the following error.

File "G:/Project/sharafat/oops/", line 14, in <module>
AttributeError: type object 'student' has no attribute '__private_method'
create method in python

This Post Has 2 Comments

Comments are closed.