Professional Documents
Culture Documents
CS101 6212140ab42e8
CS101 6212140ab42e8
CS101 6212140ab42e8
CHAPTER 4
Functions
A function in python is a block of code which will execute
whenever it is called. We can pass parameters in the functions as
well. To understand the concept of functions, lets take an example.
Instead, what you can do is, write the logic in a function. Call that
function every time you need to calculate the factorial. This will
reduce the complexity of your code and save your time as well.
Run
Let us see from the coding perspective that how do you instantiate a class
and an object.
Class is defined under a Class Keyword.
Run
Explanation: emp1 and emp2 are the objects that are instantiated against the
class employee .Here, the word (__dict__) is a dictionary which prints all the values
of object emp1 against the given parameter (name, age, salary). (__init__) acts like
a constructor that is invoked whenever an object is created.
FUNDAMENTALS OF PROGRAMMING LANGUAGES, NASHWAN A. OTHMAN, 2022
Run
Run
__init__ is a special method in Python classes, it is the constructor method for a class.
In the following example you can see how to use it. __init__ is called when ever an
object of the class is constructed. So, it allows the class to initialize the attributes of the
class.
init is short for initialization.
self
The self in keyword in Python is used to all the instances in a class. By using
the self keyword, one can easily access all the instances defined within a
class, including its methods and attributes.