Introduction to OOP in Python

Posted on

A class is a concept that combines data and functionality within a singular entity. When a class is formed, it essentially generates a new type. By instancing that class, a new object of that particular type is produced. Each object, arising from an instance of the class, can possess attributes to control its state and is equipped with methods to modify its state or information.

A. CLASS

A class in Python is a syntax that encompasses all the typical elements of Object-Oriented Programming, often abbreviated as OOP. Similar to how a function is defined using the “def” syntax, a class definition employs the “class” syntax. However, for the class to become operational and impact the program, it must be executed or called initially.

A class itself supports two kinds of operations:
– Refers to attributes.
– Instantiation.
To make it clearer, we will discuss using the following example.

there is an attribute definition i and a function definition j. The process of referring to attributes namely Counting.i and Counting.j by will return integer and function values. Of course, you can also change its value, for example by providing another integer to the Counting.i will change the current value.

B. Object: an instance of a class

The next discussion is the instantiation of a class, using function notation, namely with opening brackets and closing brackets, will produce an object. Then the results of this instantiation are usually stored in a variable with a representative name. The following is an example of creating an instance of the Counting class to produce an object.

As a result of a class instance, an object has attributes and methods obtained from the class. A method is a special function that “belongs” to an object. To call method j from object k, the results of the instance of the Counting class above are as follows.

A method is a special function, the function j in the Counting class definition has one argument called self, whereas the method call from object k above does not use an argument. If j is a regular function in Python, of course this call will return an error.

C. Class Constructor

You often encounter the need to set the initial value or initial conditions of the attributes of the class, so for this need a special function is used which is usually called a constructor. In Python, this special function or method as a constructor is called __init__. When instantiating a class, the __init__ method will automatically be called first. The following is the definition of the Counting class above if modified using the constructor.

The value of attribute i is not defined at the beginning of the Counting definition, after instantiation the value of attribute i will be 12345. Although you can define variable i as an attribute of the Counting class, you should be careful about variables that will be shared for all instances of classes, especially for types that can change (mutable), for example lists and dictionaries.

Of course, to support more dynamic applications, the constructor can have parameters that can be sent during the instantiation process, there can even be more than one parameter if specified

example of calling object:

D. Methods

3 types of methods: methods of objects (object methods), methods from class (class method), and Static method (static method).

In general, a method of object is a special function that “belongs” to an object, namely the result of the instantiation of a class. The first argument of methods in a class is usually given the name self as a naming convention or standard, although you can also use other names. Even in Python there is no special meaning about this self syntax, but it is highly recommended to use this conversion so that the Python program you create will be easier for other programmers to understand. As you might expect, for a method, it is actually passed an object (the resulting instance of the class) as its first argument, in this case called self.

For example, if k is the result object of an instance of the Counting class, when calling the method j. k.j() equivalent to Counting.j(k) The self argument to the j method will be filled with the resultant object of the Calculator class.

There are two built-in functions that we will discuss, namely classmethod and staticmethod. Classmethod is a function that converts a method into a class method. This function is used as a @classmethod decorator function, then the call can be made directly from the defined class or via an object.

To make a call from a class, do it as follows, where the first argument cls has been entered into the Counting class.

Staticmethod is a function that converts a method into a static method. In use, this function is used as a @staticmethod decorator function, then the call can be made directly from the defined class or via an object. Static methods do not accept input first arguments implicitly.

Calling a class is like calling a normal function.

Static methods can also be called from objects, resulting from the instantiation of the Calculator class, similar to ordinary function calls even though they are called from objects.

Reference: https://docs.python.org/id/3.8/tutorial/classes.html#class-and-instance-variables

Leave a Reply

Your email address will not be published. Required fields are marked *