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
a87150
V2EX  ›  Python

Python 如何用一个变量存储多个参数而又不变成 tuple?

  •  
  •   a87150 · 2017-09-20 17:42:02 +08:00 · 3216 次点击
    这是一个创建于 2403 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有一个函数支持传入多个参数,但是不支持 tuple 或者别的数据结构储存的参数,如何一次性传入多个参数?

    f(1,2,3) #ok
    
    a = 1,2,3
    
    f(a)     #no
    
    6 条回复    2017-09-20 22:11:02 +08:00
    wwwjfy
        1
    wwwjfy  
       2017-09-20 17:47:58 +08:00   ❤️ 2
    f(*a)
    janxin
        2
    janxin  
       2017-09-20 17:48:44 +08:00   ❤️ 1
    你这个函数根据描述不应该是这样啊

    In [1]: def func(a, b, c):
    ...: print(a,b,c)
    ...:

    In [2]: args=(1,2,3)

    In [3]: func(*args)
    1 2 3
    sfdye
        3
    sfdye  
       2017-09-20 17:49:28 +08:00
    kwargs
    a87150
        4
    a87150  
    OP
       2017-09-20 17:52:01 +08:00
    @wwwjfy 原来这么简单
    gclove
        5
    gclove  
       2017-09-20 17:55:37 +08:00
    @a87150

    可以不具体指定有几个参数的,

    可以搜一下 python 不定长参数

    回顾下基础语法
    hcnhcn012
        6
    hcnhcn012  
       2017-09-20 22:11:02 +08:00
    def func(*args):
    pass
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2965 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 03:07 · PVG 11:07 · LAX 20:07 · JFK 23:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.