求助,为什么 randint()方法作为一个形式参数,然后进行 for 循环调用的时候每次都 random 同一个数出来 - V2EX
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 2316 次点击
    这是一个创建于 2526 天前的主题,其中的信息可能已经有所发展或是发生改变。
    # -*- 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     Solana     855 人在线 &nbp; 最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 19:31 PVG 03:31 LAX 12:31 JFK 15:31
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86