専門ユニット2/山内研セミナー(2023/10/11)
関連サイトと資料
クラスの基礎知識のサンプルプログラム, クラス変数/クラスメソッド/スタティックメソッドのサンプルプログラム, クラスを使ってスタックとキューを作成するのサンプルプログラム
Point_v1.py(version 1.0)
class Point:
pass
main1.py
from Point_v1 import Point
point1 = Point()
print(type(point1))
main1a.py
from Point_v1 import Point
point1 = Point()
print(dir(point1))
Point_v2.py(version 2.0)
class Point:
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
main2.py
from Point_v2 import Point
point1 = Point(1.0, 1.0)
point2 = Point()
print(f'point1: ({point1.x}, ({point1.y})')
print(f'point2: ({point2.x}, ({point2.y})')
main2a.py
from Point_v2 import Point
from math import sqrt
point1 = Point(1.0, 1.0)
point2 = Point()
print(sqrt((point1.x - point2.x) ** 2 + (point1.y - point2.y) ** 2))
Point_v3.py(version 3.0)
from math import sqrt
class Point:
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
def difference(self, point=None):
if not point:
point = Point() # 原点を表すPointクラスのインスタンスを生成
return sqrt((self.x - point.x) ** 2 + (self.y - point.y) ** 2)
main3.py
from Point_v3 import Point
point1 = Point(1.0, 1.0)
point2 = Point()
point3 = Point(5, 4)
print(point1.difference(point2))
print(point1.difference())
print(point3.difference(point1))
MyClass_v1.py(version 1.0)
class MyClass:
count = 0 # クラス変数countの定義
main10.py
from MyClass_v1 import MyClass
print(MyClass.count)
main10a.py
from MyClass_v1 import MyClass
instance = MyClass()
print(instance.count)
MyClass_v2.py(version 2.0)
class MyClass:
count = 0
def __init__(self):
MyClass.count += 1
print(f'you made {MyClass.count} instance(s)')
main11.py
from MyClass_v2 import MyClass
instance1 = MyClass()
instance2 = MyClass()
MyClass_v3.py(version 3.0)
class MyClass:
count = 0
def __init__(self):
MyClass.count += 1
print(f'you made {MyClass.count} instance(s)')
@classmethod # クラスメソッドの定義
def get_count(cls):
print(cls.count) # クラス変数には「cls.クラス変数」としてアクセス
main12.py
from MyClass_v3 import MyClass
MyClass.get_count()
main12a.py
from MyClass_v3 import MyClass
instance1 = MyClass()
instance2 = MyClass()
MyClass.get_count()
MyClass_v4.py(version 4.0)
class MyClass:
count = 0
def __init__(self):
MyClass.count += 1
print(f'you made {MyClass.count} instance(s)')
@classmethod # クラスメソッドの定義
def get_count(cls):
print(cls.count) # クラス変数には「cls.クラス変数」としてアクセス
@staticmethod
def static_get_count():
print('count:', MyClass.count)
main13.py
from MyClass_v4 import MyClass
MyClass.static_get_count()
instance = MyClass()
instance.static_get_count()
MyStack_v1.py(version 1.0)
class MyStack:
def __init__(self):
self.stack = [] # 空のリストをスタックに保存するデータの入れ物とする
def push(self, item):
pass # 取りあえず何もしない
def pop(self):
pass # 取りあえず何もしない
MyStack_v2.py(version 2.0)
class MyStack:
def __init__(self):
self.stack = []
def push(self, item):
self.stack.append(item)
def pop(self):
pass # 取りあえず何もしない
main20.py
from MyStack_v2 import MyStack
mystack = MyStack()
mystack.push(0) # スタックに「0」をプッシュ(リストの末尾に「0」を追加)
mystack.push(1) # スタックに「1」をプッシュ(リストの末尾に「1」を追加)
mystack.push(2) # スタックに「2」をプッシュ(リストの末尾に「2」を追加)
print(mystack.stack) # MyStackクラスのインスタンスのインスタンス変数の値を表示
MyStack_v3.py(version 3.0)
class MyStack:
def __init__(self):
self.stack = []
def push(self, item):
self.stack.append(item)
def pop(self):
result = self.stack[-1] # 末尾の要素を変数に取り出す
del self.stack[-1] # リストから要素を削除する
return result # リスト末尾から取り出したデータを返送する
main21.py
from MyStack_v3 import MyStack
mystack = MyStack()
mystack.push(0)
mystack.push(1)
print(mystack.pop())
print(mystack.pop())
MyStack_v4.py(version 4.0)
class MyStack:
def __init__(self):
self.stack = []
def push(self, item):
self.stack.append(item)
def pop(self):
return self.stack.pop()
main22.py
from MyStack_v4 import MyStack
mystack = MyStack()
mystack.push(0)
mystack.push(1)
print(mystack.pop())
print(mystack.pop())
main22a.py
from MyStack_v4 import MyStack
mystack = MyStack()
mystack.push(0)
mystack.push(1)
print(mystack.pop())
print(mystack.pop())
print(mystack.pop())
MyStack_v5.py(version 5.0)
class MyStack:
def __init__(self):
self.stack = []
def push(self, item):
self.stack.append(item)
def pop(self):
if len(self.stack) == 0:
return None
return self.stack.pop()
main23.py
from MyStack_v5 import MyStack
mystack = MyStack()
mystack.push(0)
print(mystack.pop())
print(mystack.pop())
MyQueue_v1.py(version 1.0)
class MyQueue:
def __init__(self):
self.queue = []
def enqueue(self, item):
self.queue.append(item)
def dequeue(self):
if len(self.queue) == 0:
return None
result = self.queue[0]
del self.queue[0]
return result
main24.py
from MyQueue_v1 import MyQueue
myq = MyQueue()
myq.enqueue(0)
myq.enqueue(1)
print(myq.dequeue())
print(myq.dequeue())
print(myq.dequeue())
main23a.py
from MyStack_v5 import MyStack
mystack = MyStack()
print(str(mystack))
print(repr(mystack))
MyStack_v6.py(version 6.0)
class MyStack:
def __init__(self, *args):
self.stack = []
for item in args:
self.stack.append(item)
def push(self, item):
self.stack.append(item)
def pop(self):
if len(self.stack) == 0:
return None
return self.stack.pop()
main25.py
from MyStack_v6 import MyStack
mystack = MyStack(1, 2, [3, 4])
print(mystack.stack)
MyStack_v7.py(version 7.0)
class MyStack:
def __init__(self, *args):
self.stack = []
for item in args:
self.stack.append(item)
def push(self, item):
self.stack.append(item)
def pop(self):
if len(self.stack) == 0:
return None
return self.stack.pop()
def __repr__(self):
return 'MyStack(' + repr(self.stack) + ')'
main26.py
from MyStack_v7 import MyStack
mystack = MyStack(1, 2, [3, 4])
print(repr(mystack))
print(mystack)
MyStack_v8.py(version 8.0)
class MyStack:
def __init__(self, *args):
self.stack = []
for item in args:
self.stack.append(item)
def push(self, item):
self.stack.append(item)
def pop(self):
if len(self.stack) == 0:
return None
return self.stack.pop()
def __repr__(self):
return 'MyStack(' + repr(self.stack) + ')'
def __str__(self):
return str(self.stack)
main27.py
from MyStack_v8 import MyStack
mystack = MyStack(1, 2, [3, 4])
print(repr(mystack))
print(mystack)
MyStack_v9.py(version 9.0)
class MyStack:
def __init__(self, *args):
self.stack = []
for item in args:
self.stack.append(item)
def push(self, item):
self.stack.append(item)
def pop(self):
if len(self.stack) == 0:
return None
return self.stack.pop()
def __repr__(self):
return 'MyStack(' + repr(self.stack) + ')'
def __str__(self):
return str(self.stack)
def __iter__(self):
return iter(self.stack)
main28.py
from MyStack_v9 import MyStack
mystack = MyStack(1, 2, [3, 4])
for item in mystack:
print(item)
MyStack_v10.py(version 10.0)
class MyStack:
def __init__(self, *args):
self.stack = []
for item in args:
self.stack.append(item)
def push(self, item):
self.stack.append(item)
def pop(self):
if len(self.stack) == 0:
return None
return self.stack.pop()
def __repr__(self):
return 'MyStack(' + repr(self.stack) + ')'
def __str__(self):
return str(self.stack)
def __iter__(self):
return iter(self.stack)
def __getitem__(self, key):
return self.stack[key]
main29.py
from MyStack_v10 import MyStack
mystack = MyStack(1, 2, [3, 4])
print(mystack[0])
print(mystack[0:2]) # スライスできるか?