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

求助,为什么 randint()方法作为一个形式参数,然后进行 for 循环调用的时候每次都 random 同一个数出来

  •  
  •   Alerta · 2018-11-12 10:29:49 +08:00 · 1782 次点击
    这是一个创建于 1964 天前的主题,其中的信息可能已经有所发展或是发生改变。
    # -*- coding:utf-8 -*-
    import random
    import collections
    result = {
        1: 0,
        2: 0,
        3: 0,
        4: 0,
    }
    
    
    def test(b=random.randint(1, 5)):
        global result
        if b == 1:
            result[1] += 1
        elif b == 2:
            result[2] += 1
        elif b == 3:
            result[3] += 1
        elif b == 4:
            result[4] += 1
    
    
    if __name__ == "__main__":
        for i in range(1000):
            test()
    
        print(result)
    
    {1: 1000, 2: 0, 3: 0, 4: 0}
    

    每次都只是不同的一个数字跑 1000 遍,感觉像是 list 作为形参的时候,可变对象的问题。但是 randint 本身打印出来就是一个 int 类型,求助大佬指教指教

    8 条回复    2018-11-12 11:27:26 +08:00
    xpresslink
        1
    xpresslink  
       2018-11-12 10:33:41 +08:00
    你这么写
    def test(b=random.randint(1, 5)):
    等同于
    b=random.randint(1, 5)
    def test(b):
    Alerta
        2
    Alerta  
    OP
       2018-11-12 10:42:39 +08:00
    @xpresslink 我也是这样改的,但就是想知道为啥,求指教
    ChristopherWu
        3
    ChristopherWu  
       2018-11-12 10:47:33 +08:00
    @Alerta

    debug 路径是这样子的:

    这个程序跟其他程序有什么区别-> 用了默认参数 -> 不用默认参数显式传程序就没有问题 -> 查查 python 默认参数 是怎么样做的 -> https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001374738449338c8a122a7f2e047899fc162f4a7205ea3000 -> Python 函数在定义的时候,默认参数 L 的值就被计算出来了 -> 所以 def test(b=random.randint(1, 5))的函数原型是 def test(b=一跑完这个函数定义后取到的 1-5 的随机值) -> 所以你每次调默认参数都是固定的。

    问题解决。
    no1xsyzy
        4
    no1xsyzy  
       2018-11-12 10:48:23 +08:00
    def 的时候默认值就计算完毕了,会永久存储
    比如
    def pusher(item, tow=[]):
    tow.append(item)
    return tow
    pusher(1)
    pusher(2)
    jheroy
        5
    jheroy  
       2018-11-12 10:50:19 +08:00
    Python ’ s default arguments are evaluated once when the function is defined, not each time the function is called (like it is in say, Ruby). This means that if you use a mutable default argument and mutate it, you will and have mutated that object for all future calls to the function as well.
    no1xsyzy
        6
    no1xsyzy  
       2018-11-12 10:50:47 +08:00
    Alerta
        7
    Alerta  
    OP
       2018-11-12 11:27:12 +08:00
    @ChristopherWu 真的很感谢,不仅仅是感谢提供的答案,更重要的是提供了解决问题的思路,真的很棒
    Alerta
        8
    Alerta  
    OP
       2018-11-12 11:27:26 +08:00
    @no1xsyzy 感谢感谢 明白了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4558 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:57 · PVG 17:57 · LAX 02:57 · JFK 05:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.