Study from Python Hour7: How to use classes
Study from Python Hour7: How to use classes
- When I tried to do something with Python, I first searched for a sample script and somehow executed it.
- 7 hours I want to hit REST API with automatic execution, check the result, and even operate the VM
- You could already "use" the class, but let's think about "design" as well.
Learning materials
- Learn the basics in just one day! A super introduction to Python
- https://a.r10.to/hbHMiv
Past posts
environment
- Windows
- Python Ver3 series
Purpose of using the class
- A class is a grammar for writing a program that is easier to understand by neatly organizing complicated processes.
- Easier to understand = more readable, easier to maintain, easier to reuse
- You can code without using classes, but it requires a lot of variables and a lot of functions, and you will not be rewarded for your efforts.
- Regarding variables, "emp_name" was initially created as a variable for general employees (implicitly). Therefore, if you use it for executives, it may cause an error in the code later, and there are cases where the developer unintendedly uses it. You can use classes to force them to follow using the program's grammar.
About class grammar
- Classes are created as follows.
- Class and method definitions
- Definition of data (instance variable)
- A class is a blueprint for creating an instance, and an instance is an entity created from it. Therefore, the class defines what kind of internal data and methods should be given to the instance.
- For the time being, the class looks like this (not an object-oriented description method)
- You can define the class in the script, but small scripts are fine, but it is recommended to create it as a separate file and separate it from the main script file.
Importされるファイル(読み込まれる側のファイル)mymodule.py
class MyClass:
def plus(self, a, b):
return a + b
def minus(self, a, b):
return a - b
File to import (file to be executed) sample.py
import mymodule #Declare the file to read (no extension required)
my_ins = mymodule.MyClass() #Instantiation of MyClass
a = my_ins.plus(5, 3) #Use MyClass methods
print(a)
b = my_ins.minus(5, 3)
print(b)
Execution result
C:\script>sample.py
8
2
C:\script>
Programs that use classes
- Bingo game program
- Randomly extract numbers from 1 to 99 (no duplication, of course)
Bingo class bingo.py
import random
class Bingo:
def __init__(self):
self.balls = list(range(1, 100))
def get_ball(self):
random.shuffle(self.balls)
return self.balls.pop()
def has_ball(self):
return len(self.balls) !=0
File to import (file to be executed) bingo_go.py
import bingo
bingo = bingo.Bingo()
while bingo.has_ball():
print(bingo.get_ball())
Execution result
C:\script>bingo_go.py
71
15
69
5
27
76
・
・
・
0 to 99 values are randomly retrieved
- Isn't the code pretty neat?
- I think that the passing of variables and data is clarified by creating a class, so I think that it is not necessary to check the value of extra variables and debugging is easy.
This summary
-
Creating a class is easy, but organized.
-
Instantiating from a class makes the data easier to work with.
-
It's far from object-oriented coding, but I was able to create a class that is easy to reuse.
-
I wonder if a good class can be created by designing while thinking about what kind of movement you want to do.
For Quotations / Lightning Talk
- I know how to use it, but it's far from making something. .. .. .. .. ..