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).
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