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

Byte of Python 可变参数部分的疑问?

  •  
  •   twitterpig · 2017-05-20 20:09:44 +08:00 · 2318 次点击
    这是一个创建于 2534 天前的主题,其中的信息可能已经有所发展或是发生改变。

    原始代码: def total(a=5, *numbers, **phonebook): print('a', a)

    #遍历元组中的所有项目
    for single_item in numbers:
    	print('single_item', single_item)
    
    #遍历词典中的所有项目
    for first_part, second_part in phonebook.items():
    	print(first_part, second_part)
    

    print(total(10,1,2,3,Jack=1123,John=2231,Inge=1560))

    输出: a 10 single_item 1 single_item 2 single_item 3 Jack 1123 John 2231 Inge 1560 None [Finished in 0.1s]

    1.大🐍能给详细解读下不? 2.我的输入答案的( Jack 1123 John 2231 Inge 1560 )与书上相反( Inge 1560 John 2231 Jack 1123 ),不知道为什么? 3.为什么输出最后会出现一个:None ? 4.如何在 sublime 中输入*,这种上标的星号?同样的操作,sublime 中是平行的星号。

    9 条回复    2017-05-21 16:54:46 +08:00
    264768502
        1
    264768502  
       2017-05-20 20:16:10 +08:00 via Android   ❤️ 1
    2. 字典无序
    3. 函数无指定返回,所以返回了 None
    4. 目的何在? 想说的是字体?
    twitterpig
        2
    twitterpig  
    OP
       2017-05-20 20:27:49 +08:00
    @264768502 我自己再品味,品味。
    --------------- *numbers, **phonebook 属于左上标的星号,但是我在 sublime 中输入显示的是* numbers,** phonebook,
    yexiaoxing
        3
    yexiaoxing  
       2017-05-20 20:32:18 +08:00   ❤️ 1
    你多试几次就知道了,字典内部储存是 hash,无序的。
    total 函数没有返回值,默认为 None。
    检查一下你的输入法是不是开了全角符号之类的?如果开了输入法就关了吧。
    twitterpig
        4
    twitterpig  
    OP
       2017-05-20 20:36:26 +08:00
    @yexiaoxing 谢谢你的回答,即使是这种平行的**,并不影响运行。囧~我再试一试~
    billlee
        5
    billlee  
       2017-05-21 00:23:28 +08:00
    * 那个是字体问题吧。。
    ericls
        6
    ericls  
       2017-05-21 00:39:26 +08:00 via iPhone
    @yexiaoxing 3.6.1 有序
    weyou
        7
    weyou  
       2017-05-21 11:09:24 +08:00 via Android
    第一次听说“上标的”星号,还有“平行的”星号
    twitterpig
        8
    twitterpig  
    OP
       2017-05-21 16:39:34 +08:00
    @weyou 555555555,你笑我?
    twitterpig
        9
    twitterpig  
    OP
       2017-05-21 16:54:46 +08:00
    @ericls 我确实用的是 3.6.1 版本的 Python。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   885 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 20:30 · PVG 04:30 · LAX 13:30 · JFK 16:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.