herbivory
V2EX  ›  Python

动态修改类的方法的参数数量

By herbivory at 2018 年 9 月 13 日 · 2451 次点击

class A: def a(self,z,t): return self.c()+z+t def c(self): return 1 我的方法是 A.a = MethodType(getattr(A,"a"),A) 不过提示 TypeError: c() missing 1 required positional argument: 'self' 不知道大神们怎么想

3 条回复  •  2018-09-22 23:13:07 +08:00
xpresslink
   1
xpresslink  
   2018 年 9 月 13 日
你的方法是什么鬼?
python 根本没有这种实际需求,因为语言特性本身就支持*arg 和**kwarg 这种变长参数。
herbivory
   2
herbivory  
OP
   2018 年 9 月 22 日
@xpresslink 666,
@xpresslink 666,垃圾的人就只是说没有,你见过吗你就说没有
xpresslink
   3
xpresslink  
   2018 年 9 月 22 日
>>> import types
>>> help(types.MethodType)
Help on class method in module builtins:

class method(object)
| method(function, instance)
|
| Create a bound instance method object.
推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
© 2026 V2EX · 58ms · 3.9.8.5