V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2018-09-13 21:27:23 +08:00 · 1484 次点击
    这是一个创建于 2044 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    class method(object)
    | method(function, instance)
    |
    | Create a bound instance method object.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5159 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 01:20 · PVG 09:20 · LAX 18:20 · JFK 21:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.