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
from Person import Person kawasaki = Person('kawasaki', 250) isshiki = Person('isshiki', 19) kawasaki.hello() print(isshiki.get_age())
from Person import Person class Student(Person): pass
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() # 基底クラスのメソッドを呼び出す
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
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
from Student_v3 import Student isshiki = Student('isshiki', 18, 'Imperial univ') isshiki.hello() print(isshiki.get_school())
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)
from Student_v4 import Student isshiki = Student('isshiki', 18, 'Imperial univ') isshiki.hello()
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)))
class MyStack: def __init__(self): self.stack = [] def push(self, item): self.stack.append(item) def pop(self): return self.stack.pop()
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]
class MyStack3(list): def push(self, item): self.append(item)
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:])
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))
mylist = list([1, 2, 3]) # 1、2、3を要素とするリストを生成 print(mylist) mylist = list(1, 2, 3) # エラー(引数は0個か1個だけ)
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個だけ)
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)
from MyStack3_v2 import MyStack3 mystack = MyStack3() mystack2 = mystack.copy() print(type(mystack2))
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)
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)