[Introduction to Python] How to use class in Python?

Reference site: [Introduction to Python] How to use class in Python?

Python is a language that supports object-oriented programming. To do this, you need to define and create a class. Python class definitions are easier than in other languages.

This time, I will explain how to handle basic classes in Python.

Class definition

Python uses class statements to define classes. The definition method using the class statement is as follows.

class class name:

After the class statement, add the class name that will be the name of the class, and add ":" at the end. After that, add a paragraph indent and define the class.

class Test:
    pass  #Enter pass for a class that does nothing

test = Test()  #Create an instance

Method

You can define methods in the class. The method is defined as follows.

class class name:
    
def method name(self):
        #Method definition

A method always has one or more arguments. Also, the first of the arguments is supposed to be named self. To call the method:

instance.Method name()
class Test:


    def test_print(self):
        print('This is test')
        
test = Test()      #Instance generation
test.test_print()  #Method call

Execution result

This is test

Constructor and destructor

Among the methods, the method that is automatically called when an instance is created is called a constructor. To define the constructor, create a method named "init".

class Test:
    def __init__(self,Argument 1,Argument 2, ….):
        #Constructor definition

init also always requires the argument self. Other arguments can be attached, and the arguments are passed when calling the class when creating an instance.

class Test:
    def __init__(self, num):
        self.num = num;  #Store the argument in the "num" variable of this class
        
    def print_num(self):
        print('The number passed as an argument is{}is.'.format(self.num)) 
    
test = Test(10)   #The argument passed here is__init__Passed to num of
test.print_num() 

Execution result

The number passed as an argument is 10.

Contrary to the constructor, the method that is called when an instance is no longer needed and is deleted is called a destructor. The destructor is defined by a method named "del".

class Test:
    def __init__(self):
        print('The constructor was called')

    def __del__(self):
        print('Destructor was called')


test = Test()  #Create an instance
del test      #Delete instance

Execution result

The constructor was called Destructor was called

Inheritance

Python classes can also extend and extend from other classes. When inheriting a class, specify the parent class in the class statement. Use super () to call the methods of the parent class.

class Test:
    def __init__(self, num):
        self.num = num;
        
    def print_num(self):
        print('The number passed as an argument is{}is.'.format(self.num))


class Test2(Test):  #Inherit the Test class
    def print_test2_info(self):
        print('This class inherits from the Test class.')
        super().print_num()  #Parent class print_num()Call

test = Test2(10)
test.print_test2_info()

Execution result

This class inherits from the Test class. The number passed as an argument is 10.

Recommended Posts

[Introduction to Python] How to use class in Python?
How to use __slots__ in Python class
How to use SQLite in Python
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
How to use regular expressions in Python
How to use is and == in Python
How to use the __call__ method in a Python class
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
How to use the C library in Python
[Introduction to Udemy Python3 + Application] 23. How to use tuples
python3: How to use bottle (2)
[Python] How to use list 1
How to use Python Image Library in python3 series
Summary of how to use MNIST in Python
How to use Python argparse
[Introduction to Python] How to use the in operator in a for statement?
Python: How to use pydub
[Python] How to use checkio
How to use tkinter with python in pyenv
How to develop in Python
[Python] How to use input ()
[Introduction] How to use open3d
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
[For beginners] How to use say command in python!
[Introduction to Python] How to use while statements (repetitive processing)
How to use bootstrap in Django generic class view
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
I tried to summarize how to use pandas in python
[Introduction to Udemy Python3 + Application] 30. How to use the set
How to use the model learned in Lobe in Python
How to write a Python class
[Python] How to do PCA in Python
Python: How to use async with
How to use classes in Theano
[Python] How to use Pandas Series
How to collect images in Python
How to use Requests (Python Library)
[Introduction to Python] How to parse JSON
[Introduction to Python] Let's use pandas
[Python] How to use list 3 Added
How to use OpenPose's Python API
How to wrap C in Python
How to use FTP with Python
Python: How to use pydub (playback)
[Introduction to Python] Let's use pandas
How to use python zip function
[Introduction to Python] Let's use pandas
How to handle Japanese in Python
[Python] How to use Typetalk API
Comparison of how to use higher-order functions in Python 2 and 3
How to use calculated columns in CASTable
[Python] Summary of how to use pandas
How to access environment variables in Python
How to dynamically define variables in Python
How to install and use pandas_datareader [Python]