Object oriented in python

Object oriented in Python

Three major elements of object orientation

Encapsulation

It is to combine related data and operations on it into one object, and to provide information disclosure and minimum operations to the outside. Of the variables and functions of an object, data that does not need to be directly referenced from the outside and operations that are not used must be protected and concealed. Protecting means not using or rewriting outside that class. This maintains loose coupling between programs and facilitates maintenance. In addition, it is possible to prevent errors such as different types from appearing and to protect data. This is especially important when building large-scale systems.

Polymorphism (diversity)

It means that the same function or variable can behave differently depending on the type of data. If you create a method that matches the type, the program will be very long.

Inheritance

Creating a new child class by taking over the members and methods of a class. By inheriting, common parts can be reused together. There is a special kind of inheritance, which is the following two inheritances.

Abstract class

Abstract classes cannot be instantiated and will only function as a program if they are inherited. Abstract classes allow you to implement methods and declare abstract methods. The declared abstract method must be implemented in the inheritance destination.

interface

No program is written here, only the method is declared. The inherited class must always implement that method. Also, normal inheritance and abstract class inheritance do not allow multiple classes to be inherited, but interfaces can do multiple inheritance. By separating the interface, you can choose whether to inherit the part or not.

Differences between abstract classes and interfaces

Abstract classes can implement methods. The interface can be multiple inherited. Another way to look at it is that abstract classes are internal and interfaces are external. When inheriting an abstract class, it is often completed there, and when inheriting an interface, the instance is often used externally.

In python

Encapsulation

In Python, all variables and methods are public, so they are distinguished by an underscore_. Variables and methods with the first underscore are treated as private.


Class ClassName():
    def __init__(self):
        self.public_variable = ‘safe’
        self._private_variable = ‘unsafe’

    def public_method():
        pass

    def _private_method():
        pass

Abstract class

In python, the Abstract Base Class (ABC) is used.


class BaseClassName(metaclass=ABCMeta):
    
    @abstractmethod
    def abst_method_name(self):
        pass
    
    def method_name(self):
        print(self.name)

Specify ABCMeta as the metaclass and add the @abstractmethod decorator to the abstract method definition. This ensures that inherited classes override abst_method_name.

interface

Since python does not have an interface as standard and multiple inheritance is possible, you can only make something like an interface. If you want to make something like that, define the above abstract class without implementation.

Recommended Posts

Object oriented in python
String object methods in Python
Null object comparison in Python
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Python built-in object
Meta-analysis in Python
Unittest in python
Discord in Python
DCI in Python
quicksort in python
nCr in python
Python built-in object
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Create a JSON object mapper in Python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python
Daily AtCoder # 33 in Python