開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART Ⅵ.(Classes and OOP)、CHAPTER 31(Designing with Classes)、Test Your Knowledge: Quiz.1.~5.を解いてみる。
その他参考書籍
Test Your Knowledge: Quiz.1.~5.
-
コード(BBEdit)
sample.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- class Sup1: def meth1(self): print('Hello, Sup1!') class Sup2: def meth2(self): print('Hello, Sup2!') # 複数のクラスを継承 class Sub(Sup1, Sup2): pass o = Sub() o.meth1() o.meth2()
入出力結果(Terminal)
$./sample.py Hello, Sup1! Hello, Sup2! $
-
コード(BBEdit)
sample.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- class Sup: def meth1(self): print('delegation') self.meth2() class Sub1(Sup): def meth2(self): print('Hello, Sub1 World!') class Sub2(Sup): def meth2(self): print('Hello, Sub2 World!') if __name__ == '__main__': for i in (Sub1(), Sub2()): i.meth1()
入出力結果(Terminal)
$ ./sample.py delegation Hello, Sub1 World! delegation Hello, Sub2 World! $
-
コード(BBEdit)
sample.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- class A: def meth(self, data): print('A: {0}'.format(data)) class B: def meth(self, data): # composition a = A() a.meth(data) if __name__ == '__main__': b = B() b.meth('meth')
入出力結果(Terminal)
$ ./sample.py A: meth $
-
コード(BBEdit)
sample.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- class A: # unbound method def meth1(): print('Hello, unbound method!') # bound method def meth2(self): print('Hello, bound method!') if __name__ == '__main__': A.meth1() # unbound methodの呼び出し a = A() A.meth1() # unbound methodの呼び出し a.meth2() # bound methodの呼び出し A.meth2(a) # bound methodの呼び出し try: a.meth1() except Exception as err: print(type(err), err, err.args) try: A.meth2() except Exception as err: print(type(err), err, err.args) try: a.meth2() except Exception as err: print(type(err), err, err.args) try: a.meth2(a) except Exception as err: print(type(err), err, err.args) try: a.meth2(A) except Exception as err: print(type(err), err, err.args)
入出力結果(Terminal)
$ ./sample.py Hello, unbound method! Hello, unbound method! Hello, bound method! Hello, bound method! <class 'TypeError'> meth1() takes 0 positional arguments but 1 was given ('meth1() takes 0 positional arguments but 1 was given',) <class 'TypeError'> meth2() missing 1 required positional argument: 'self' ("meth2() missing 1 required positional argument: 'self'",) Hello, bound method! <class 'TypeError'> meth2() takes 1 positional argument but 2 were given ('meth2() takes 1 positional argument but 2 were given',) <class 'TypeError'> meth2() takes 1 positional argument but 2 were given ('meth2() takes 1 positional argument but 2 were given',) $
-
コード(BBEdit)
sample.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- # pseudoprivate attributesは名前の衝突を避けるために使われる class A: def seta(self): self.__data = '__a' # _A__data self.data = 'a' def p(self): print(self.__data, self.data) class B: def setb(self): self.__data = '__b' # # _B__data self.data = 'b' class C(A, B): pass if __name__ == '__main__': o = C() o.seta() o.p() o.setb() o.p() print(o._A__data, o._B__data)
入出力結果(Terminal)
$ ./sample.py __a a __a b __a __b $
0 コメント:
コメントを投稿