Class notation in Python

Class notation

class (name of the class):
    (Suite)

The class is written as above. As the name implies, the class name is the name of the class, and the suite is the part that describes processing such as simple statements and methods. Methods can be written in much the same notation as functions, but unlike functions, they are generally written inside a class (I don't know any more).

Actually make a class

class Ball:
    def __init__(self):
        print('Create class')

Create it like this. self is used to define a variable that belongs to that class (object), called an instance variable. Now let's define an instance variable.

class Ball:
    def __init__(self, c, s):
        self.color = c
        self.size = s
        print('Create')

Instance variables

self.(Instance variable name)

It is defined as. Also, instance variables generally seem to be defined inside the __init __ method.

Now let's create an object.

ball = Ball('RED', 20)
print(ball.color)
print(ball.size)

>>Create
>>RED
>>20
```

 Here, "Create" is displayed because print is included in the ball class.
 A method of reducing the amount of description while reusing code using such a class is called object-oriented (there is a high possibility that it is wrong).

 If you find an error, please let us know.



Recommended Posts

Class notation in Python
case class in python
Included notation in Python function arguments
Landmines hidden in Python class variables
Read PNG chunks in Python (class)
Examine the object's class in python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
[Python] class, instance
Geocoding in python
SendKeys in Python
"Kanrika" python class
Meta-analysis in Python
Unittest in python
About python, class
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort 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
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Python class, instance
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
#Python basics (class)
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Generate a first class collection in Python
Display LaTeX notation formulas in Python, matplotlib
[Introduction to Python] How to use class in Python?
Implement __eq__ etc. generically in Python class
Playing card class in Python (with comparison)
How to use __slots__ in Python class
Generate a class from a string in Python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python