Functions and Methods of Python
Functions and Methods of Python
Functions of Python
A function is a block of organized, reusable code that is used to perform a
single, related action. Functions provide better modularity for your
application and a high degree of code reusing.
As you already know, Python gives you many built-in functions like print(),
etc. but you can also create your own functions. These functions are
called user-defined functions.
You can define functions to provide the required functionality. Here are
simple rules to define a function in Python.
Function blocks begin with the keyword def followed by the function
name and parentheses ( ( ) ).
Any input parameters or arguments should be placed within these
parentheses. You can also define parameters inside these
parentheses.
The first statement of a function can be an optional statement - the
documentation string of the function or docstring.
The code block within every function starts with a colon (:) and is
indented.
The statement return [expression] exits a function, optionally passing
back an expression to the caller. A return statement with no
arguments is the same as return None.
Instance Method
Class Method
Static Method
1. Instance Methods
The purpose of instance methods is to set or get details about instances
(objects), and that is why they’re known as instance methods. They are
the most common type of methods used in a Python class. They have one
class. Although you don’t have to pass that every time. You can change
i.e self.
2. Class Methods
The purpose of the class methods is to set or get the details (status) of
the class. That is why they are known as class methods. They can’t
access or modify specific instance data. They are bound to the class
3. Static Methods
Static methods cannot access the class data. In other words, they do not
need to access the class data. They are self-sufficient and can work on
their own. Since they are not attached to any class attribute, they cannot
get or set the instance state or class state. In order to define a static