推荐学习书目
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 · 4082 views
    This topic created in 3157 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
        1
    wwwjfy  
       Sep 20, 2017   ❤️ 2
    f(*a)
    janxin
        2
    janxin  
       Sep 20, 2017   ❤️ 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  
       Sep 20, 2017
    kwargs
    a87150
        4
    a87150  
    OP
       Sep 20, 2017
    @wwwjfy 原来这么简单
    gclove
        5
    gclove  
       Sep 20, 2017
    @a87150

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

    可以搜一下 python 不定长参数

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