Python is an Object Oriented language. That means, everything is an Object in Python. Python supports Classes and Functions. In this Article, I am going to explain, how to define & use Functions and Classes in Python. Before I start with Classes, first will discuss with Functions.
Functions
Functions are the code blocks and defined using the keyword “def“. We can call them by using the name of the function wherever required. Functions allows to pass arguments; and even we can define the Functions without the arguments or with default arguments. The arguments which are passed to the Functions, can be used within the Function’s code block.
We define the Functions, to do particular job. Once the specific purpose is done, Function will return with our without a value. Usually we place the commonly used code in the Function block; and we call the block by its name, called Function; wherever required. This avoids to write the frequently used code in the Program.
For example, I define a below function, which will display “Hello, World!” message; whenever we call the Function.
# func.py def func(): print("Hello, World!") func()
Observe that, the Function “func” is defined with the keyword “def”; without any arguments and we called the function by its name “func()”. Once we run the function, we see “Hello, World!” output on the screen.
As mentioned above, everything is an Object in Python; even the Function is also an Object. This is not the case in some languages; like “C++”.
Lets’ define another function with arguments & with default arguments. If the function has default argument means, we no need to pass a value to it; it takes default value if none of the value is passed. If you define the function with default arguments; the arguments must be placed after the formal arguments. If no formal arguments defined, the optional arguments can be placed from the beginning.
For example, below code clarifies this:
# func.py def func(): print("Hello, World!") def argfunc(arg1, optarg="World!"): print(arg1 + " - " + optarg) func() argfunc("Hello") argfunc("Hello", "Friend!")
Observe that, we called the function “argfunc” with or without default arguments.
Classes
Class enables Object Oriented concepts abstraction & encapsulation. Class contains data and the functions which operate on the data. The data members of a class can be called as class attributes and the functions defined in the class are called its’ methods.
Class definition will start with “class” keyword. To access its members, we need to create an instance of a class; called an object of a class. But it is always not required to create an object of a class; we can access its members directly and we call the members, static members. That means, to access static members of a class, no need to create an instance of a class.
For example, below class MyClass has a method “func” and called directly using the class name; instead of creating an object. We call “func” is a static method of the class MyClass.
# class.py class MyClass: def func(): print("Hello, World! from MyClass") MyClass.func()
Lets’ try to create an instance of a class and call the “func” method through the class object.
C:\Projects>python class.py
Hello, World! from MyClass
Traceback (most recent call last):
File "class.py", line 9, in
obj.func()
TypeError: func() takes 0 positional arguments but 1 was given
Notice that, we have seen an error, “TypeError“, when we call the method through class object. Why? The reason is, we can not call the static methods through class objects. If we define a class method; without any arguments; means, it is a static method. Then, how do we define a non-static method.? You must define the class method with at least a single argument. The first argument of the class reference to an object of the class.
Modify the above class to add a method and call it through the class object.
# class.py class MyClass: def func(): print("Hello, World! from MyClass") def method(this): print(this) MyClass.func() obj = MyClass() obj.method()
And when we run our program, the output looks like below:
C:\Projects>python class.py Hello, World! from MyClass <__main__.MyClass object at 0x02140CD0>
Observe the result; when we print “this” parameter of the class method “method”, it prints class object & its address. That means, first argument of the class method represents the object of the class; this is the main difference with static and non-static methods. To use the class members, we must have to get the class object and through the class object we can call its properties and methods. And we can not call non-static method without an instance of a class.
For example, if you call above method “method” directly with class name; you will get “TypeError” error.
MyClass.method()
You will see below error:
MyClass.method()
TypeError: method() missing 1 required positional argument: 'this'
We will discuss more about Python in my upcoming Articles.
/Shijit/