Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

Functions and Methods of Python

Functions in Python provide modularity and code reusability. User-defined functions are created using the def keyword followed by the function name and parameters in parentheses. The code block within a function starts with a colon and is indented. Functions can optionally return an expression using the return statement. There are three types of methods in Python: instance methods which access object details and use self as a parameter; class methods which access class details without accessing object data; and static methods which are not attached to classes or objects and do not need access to class or instance data.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Functions and Methods of Python

Functions in Python provide modularity and code reusability. User-defined functions are created using the def keyword followed by the function name and parameters in parentheses. The code block within a function starts with a colon and is indented. Functions can optionally return an expression using the return statement. There are three types of methods in Python: instance methods which access object details and use self as a parameter; class methods which access class details without accessing object data; and static methods which are not attached to classes or objects and do not need access to class or instance data.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Quilacio, Alloysius Mark A.

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.

Types of Methods in Python

There are basically three types of methods in Python:

 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

default parameter- self, which points to an instance of the

class. Although you don’t have to pass that every time. You can change

the name of this parameter but it is better to stick to the convention

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

instead of their objects.

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

method, we can use the @staticmethod decorator (in a similar way we

used @classmethod decorator). Unlike instance methods and class

methods, we do not need to pass any special or default parameters.

You might also like