|
|
发表于 2017-8-29 09:29:12
|
显示全部楼层
本楼为最佳答案
本帖最后由 ooxx7788 于 2017-8-29 09:40 编辑
- >>> class A:
- pass
- >>> A.put = lambda x : x**2
- >>> A.put(2)
- 4
- >>> a = A()
- >>> a.put(2)
- Traceback (most recent call last):
- File "<pyshell#24>", line 1, in <module>
- a.put(2)
- TypeError: <lambda>() takes 1 positional argument but 2 were given
- >>> a.put()
- Traceback (most recent call last):
- File "<pyshell#25>", line 1, in <module>
- a.put()
- File "<pyshell#21>", line 1, in <lambda>
- A.put = lambda x : x**2
- TypeError: unsupported operand type(s) for ** or pow(): 'A' and 'int'
- >>> A.put = lambda self,x:x**2
- >>> b = A()
- >>> b.put(2)
- 4
- >>> a.put(2)
- 4
- >>> A.put(2)
- Traceback (most recent call last):
- File "<pyshell#30>", line 1, in <module>
- A.put(2)
- TypeError: <lambda>() missing 1 required positional argument: 'x'
复制代码
根据以上代码可以看出,理论上应该算是可以的,但实际上,有点蛋疼的。
查看父类可以用
- >>> A.mro()
- [<class '__main__.A'>, <class 'object'>]
复制代码
直接查看子类的方法,好像没有 |
|