専門ユニット2/山内研セミナー(2023/10/17)

関連サイトと資料


クラスの継承のサンプルプログラム, リストを継承してスタックを作成するのサンプルプログラム

クラスの継承のサンプルプログラム

Person.py
class Person(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age
  
    def hello(self):
        print('Hello,', str(self.name))
  
    def get_age(self):
        return self.age
    

main1.py
from Person import Person
  
kawasaki = Person('kawasaki', 250)
isshiki = Person('isshiki', 19)
kawasaki.hello()
print(isshiki.get_age())
    


Student_v1.py(version 1.0)
from Person import Person
  
class Student(Person):
    pass
    

main2.py
from Person import Person
from Student_v1 import Student
  
kawasaki = Person('kawasaki', 250)
isshiki = Student('isshiki', 18)
print(type(kawasaki))
print(type(isshiki))
isshiki.hello()  # 基底クラスのメソッドを呼び出す
    


Student_v2.py(version 2.0)
from Person import Person
  
class Student(Person):
    def __init__(self, name, age, school):
        self.name = name
        self.age = age
        self.school = school
  
    def get_school(self):
        return self.school
    


Student_v3.py(version 3.0)
from Person import Person
  
class Student(Person):
    def __init__(self, name, age, school):
        super().__init__(name, age)
        self.school = school
  
    def get_school(self):
        return self.school
    

main3.py
from Student_v3 import Student
  
isshiki = Student('isshiki', 18, 'Imperial univ')
isshiki.hello()
print(isshiki.get_school())
    


Student_v4.py(version 4.0)
class Student(Person):
    def __init__(self, name, age, school):
        super().__init__(name, age)
        self.school = school
  
    def get_school(self):
        return self.school
  
    def hello(self):
        super().hello()
        print('You are a student of', self.school)
    

main4.py
from Student_v4 import Student
  
isshiki = Student('isshiki', 18, 'Imperial univ')
isshiki.hello()
    


main5.py
from Person import Person
from Student_v4 import Student
  
# 整数値「100」はfloat型のインスタンスか
print('isinstance(100, float):', isinstance(100, float))
  
# studentはPersonクラスのインスタンスか
student = Student('isshiki', 18, 'Imperial univ')
print('isinstance(student, Person):', isinstance(student, Person))
  
# StudentクラスはPersonクラスの派生クラスか
print('issubclass(Student, Person):', issubclass(Student, Person))
  
# PersonクラスはPersonクラスの派生クラスか
print('issubclass(Person, Person):', issubclass(Person, Person))
  
# 「100」はfloatクラスまたはstrクラスのインスタンスか
print('isinstance(100, (float, str)):', isinstance(100, (float, str)))
  
# StudentクラスはPersonクラスまたはStudentクラスの派生クラスか
print('issubclass(Student, (Person, Student)):', issubclass(Student, (Person, Student)))
    


リストを継承してスタックを作成するのサンプルプログラム

MyStack.py
class MyStack:
    def __init__(self):
        self.stack = []
  
    def push(self, item):
        self.stack.append(item)
  
    def pop(self):
        return self.stack.pop()
    


MyStack2.py
class MyStack2:
    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]
    


MyStack3_v1.py(version 1.0)
class MyStack3(list):
    def push(self, item):
        self.append(item)
    

main10.py
from MyStack3_v1 import MyStack3
  
mystack = MyStack3()
mystack.push(1)
mystack.push(2)
mystack.push(3)
mystack.push(4)
mystack.push(5)
print(mystack)
print(mystack.pop())
print(mystack.pop())
for item in mystack:
    print(item)
print(mystack[1:])
    


main11.py
from MyStack import MyStack
from MyStack2 import MyStack2
from MyStack3_v1 import MyStack3
  
print('MyStack:', dir(MyStack))
print('---')
print('MyStack2:', dir(MyStack2))
print('---')
print('MyStack3:', dir(MyStack3))
    


main12.py
mylist = list([1, 2, 3])  # 1、2、3を要素とするリストを生成
print(mylist)
mylist = list(1, 2, 3)  # エラー(引数は0個か1個だけ)
    


main13.py
from MyStack import MyStack
from MyStack3_v1 import MyStack3
  
mystack = MyStack3([1, 2, 3])  # 1、2、3を要素とするスタックを生成
print(mystack)
mystack = MyStack(1, 2, 3)  # エラー(引数は0個か1個だけ)
    


MyStack3_v2.py(version 2.0)
class MyStack3(list):
    def __init__(self, *args):
        # print(args)  # 可変長位置引数を確認したければコメントアウト
        super().__init__(args)
  
    def push(self, item):
        self.append(item)
    

main14.py

from MyStack3_v2 import MyStack3
  
mystack = MyStack3()
print(mystack)
mystack = MyStack3(1)
print(mystack)
mystack = MyStack3([1, 2, 3])
print(mystack)
mystack = MyStack3(1, 2, 3)
print(mystack)
mystack = MyStack3(1, 2, [3, 4])
print(mystack)
    


main15.py
from MyStack3_v2 import MyStack3
  
mystack = MyStack3()
mystack2 = mystack.copy()
print(type(mystack2))
    


MyStack3_v3.py(version 3.0)
class MyStack3(list):
    def __init__(self, *args):
        # print(args)
        super().__init__(args)
  
    def push(self, item):
        self.append(item)
  
    def copy(self):
        tmp = list.copy(self)
        return MyStack3(*tmp)
    

main16.py
from MyStack_v3 import MyStack3
  
mystack = MyStack3(1, 2, [3, 4])
print(mystack)
mystack2 = mystack.copy()
print(type(mystack2))
print('mystack:', mystack)
print('mystack2:', mystack2)
print('mystack == mystack2:', mystack == mystack2)
print('mystack is mystack2:', mystack is mystack2)