推荐学习书目
› 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 · Sep 20, 2017 · 4487 views
    This topic created in 3339 days ago, the information mentioned may be changed or developed.

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

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

    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
    sfdye
    Sep 20, 2017
    3
    kwargs
    a87150
    a87150
    OP
    Sep 20, 2017
    4
    @wwwjfy 原来这么简单
    gclove
    gclove
    Sep 20, 2017
    5
    @a87150

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

    可以搜一下 python 不定长参数

    回顾下基础语法
    hcnhcn012
    hcnhcn012
    Sep 20, 2017
    6
    def func(*args):
    pass
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2312 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 12:39 · PVG 20:39 · LAX 04:39 · JFK 07:39
    ♥ Do have faith in what you're doing.