V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · 2156 views
    This topic created in 2783 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   ·   5604 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 07:08 · PVG 15:08 · LAX 00:08 · JFK 03:08
    ♥ Do have faith in what you're doing.