Pharmaceutical company researchers summarized classes in Python

Introduction

Here, we will explain the basics of "classes" for beginners in Python. It is assumed that you are using Python 3 series.

Creating a class

Use class when creating a class.

class_1.py


class Human:
    name = 'Yukiya'
    def say_hello():
        return 'Hello.'

    
print(Human.name)
print(Human.say_hello())

You can create a class as described above, but with this writing method, you cannot create an instance other than "Yukiya" for name.

Use __init__ to allow you to freely set the name later. The first argument is self.

class_2.py


class Human:
    def __init__(self, n):
        self.name = n
    
    def say_hello(self):
        return 'Hello,' + self.name + 'is.'

    
yukiya = Human('Yukiya')
takada = Human('At most')

print(yukiya.name)
print(yukiya.say_hello())
print(takada.name)
print(takada.say_hello())

Class inheritance

"Inheritance" is to create a "child class" with additional functions based on an existing "parent class".

class_3.py


class Human:
    def say_hello(self):
        return 'Hello'
    

class Japanese(Human):
    def say_konnichiwa(self):
        return 'Hello.'
    

yukiya = Japanese()

print(yukiya.say_hello()) #You can also use methods of the Human class.
print(yukiya.say_konnichiwa())

Summary

Here, I explained the basics of "classes" in Python. It's a necessary idea for object-oriented programming, so make sure you understand it.

Reference materials / links

What is the programming language Python? Can it be used for AI and machine learning?

Recommended Posts

Pharmaceutical company researchers summarized classes in Python
Pharmaceutical company researchers summarized functions in Python
Pharmaceutical company researchers summarized variables in Python
Pharmaceutical company researchers summarized regular expressions in Python
Pharmaceutical company researchers summarized file scanning in Python
Pharmaceutical company researchers summarized Python control statements
Pharmaceutical company researchers summarized Python unit tests
Pharmaceutical company researchers summarized Python exception handling
Pharmaceutical company researchers summarized Python coding standards
Pharmaceutical company researchers have summarized the operators used in Python
Pharmaceutical company researchers summarized SciPy
Pharmaceutical company researchers summarized RDKit
Pharmaceutical company researchers summarized scikit-learn
Pharmaceutical company researchers summarized web scraping using Python
Pharmaceutical company researchers summarized Pandas
Pharmaceutical company researchers summarized database operations using Python
Pharmaceutical company researchers summarized NumPy
Pharmaceutical company researchers summarized Matplotlib
Pharmaceutical company researchers summarized Seaborn
Pharmaceutical company researchers summarized Python's comprehensions
Pharmaceutical company researchers summarized Python's data structures
How to install Python for pharmaceutical company researchers
Python classes learned in chemoinformatics
A pharmaceutical company researcher summarized the basic description rules of Python
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
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