test_methods.py
来自「mallet是自然语言处理、机器学习领域的一个开源项目。」· Python 代码 · 共 63 行
PY
63 行
# Python test set -- part 7, bound and unbound methodsfrom test_support import *print_test('Bound and unbound methods (test_methods.py)', 1)class A: def one(self): return 'one'class B(A): def two(self): return 'two'class C(A): def one(self): return 'another one'a = A()b = B()c = C()print_test('unbound method equality', 2)assert A.one == B.oneassert A.one <> C.oneprint_test('method attributes', 2)assert A.one.im_func == a.one.im_funcassert a.one.im_self == aassert a.one.im_class == Aassert b.one.im_self == bassert b.one.im_class == Bprint_test('unbound method invocation w/ explicit self', 2)assert A.one(b) == 'one'assert B.two(b) == 'two'assert B.one(b) == 'one'assert A.one(c) == 'one'assert C.one(c) == 'another one'assert A.one(a) == 'one'try: B.one(a) assert 0except TypeError: passtry: C.one(a) assert 0except TypeError: passprint_test('"unbound" methods of builtin types', 2)w = [1,2,3].appendx = [4,5,6].appendassert w <> xassert w.__self__ <> x.__self__y = w.__self__[:]z = x.__self__[:]assert y.append.__self__ <> wz.append(7)assert z == (x.__self__+[7])
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?