推荐学习书目
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
herbivory
V2EX  ›  Python

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

  •  
  •   herbivory · Sep 13, 2018 · 2247 views
    This topic created in 2827 days ago, the information mentioned may be changed or developed.

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

    class method(object)
    | method(function, instance)
    |
    | Create a bound instance method object.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3295 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 11:11 · PVG 19:11 · LAX 04:11 · JFK 07:11
    ♥ Do have faith in what you're doing.