2017-03-13から1日間の記事一覧

メソッドのオーバーライドとsuper

入門Python3 6.4 継承時に基底クラスのメソッドをオーバーライドすることができます class Foo(): def hello(self): print("foo hello") class Bar(Foo): # Foo.helloをオーバーライド def hello(self): print("bar hello") bar = Bar() bar.hello() $ py ma…

クラスの継承

入門Python3 6.3 続いてはクラスの継承です。 class Foo(): def __init__(self,str): self.str = str def hello(self): print(self.str) class Bar(Foo): pass bar = Bar("hello world!") bar.hello() $ py main.py hello world!BarクラスがFooクラスを継承…

Pythonのselfについて

入門Python3 6.7 Pythonではクラスのメソッドにてselfを受け取る必要がある class Foo(): # 第一引数にselfを受け取る必要がある def hello(self): print("foo hello") foo = Foo() foo.hello() $ py main.py foo hello もしselfを指定しない場合これはエラ…