Study from Python Hour7: How to use classes

Study from Python Hour7: How to use classes

Learning materials

Past posts

environment

Purpose of using the class

About class grammar

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

This summary

For Quotations / Lightning Talk

Recommended Posts

Study from Python Hour7: How to use classes
python3: How to use bottle (2)
[Python] How to use list 1
Study from Python Hour4: Object-oriented ②
Study from Python Hour3: Functions
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
Study from Python Hour4: Object-oriented ①
[Python] How to use input ()
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
How to use SWIG from waf
How to use classes in Theano
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
Study from Python Hour2: Control statements
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
How to access wikipedia from python
How to use python zip function
[Python] How to use Typetalk API
Let's use Watson from Python! --How to use Developer Cloud Python SDK
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
How to install and use pandas_datareader [Python]
How to update Google Sheets from Python
[TF] How to use Tensorboard from Keras
[python] How to use __command__, function explanation
I want to use jar from python
[Python] How to use import sys sys.argv
[Python] Organizing how to use for statements
Memorandum on how to use gremlin python
[Python2.7] Summary of how to use unittest
How to access RDS from Lambda (python)
python: How to use locals () and globals ()
How to use __slots__ in Python class
How to use "deque" for Python data
How to use Python zip and enumerate
[Python] Understand how to use recursive functions
Summary of how to use Python list
How to use regular expressions in Python
[Python2.7] Summary of how to use subprocess
How to use is and == in Python
[Blender x Python] How to use modifiers
[Question] How to use plot_surface of python
How to use xml.etree.ElementTree
How to use Python-shell
How to use the C library in Python
How to use tf.data
How to use virtualenv
How to use Seaboan