Python 变量赋值 没有成功,偶发性现象 诡异 - 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
smallgoogle
V2EX    Python

Python 变量赋值 没有成功,偶发性现象 诡异

  •  
  •   smallgoogle 2020-04-21 16:32:01 +08:00 1771 次点击
    这是一个创建于 2016 天前的主题,其中的信息可能已经有所发展或是发生改变。

    原始代码:

    def get_http_ip(): res = requests.get('http://www.xxx.com') res = json.loads(res.text) if res['code'] == 200: print(res['data']) # 这里打印结果都还是正常的 time.sleep(5) # 我猜有可能有人说太快了,那么我这里给一个定时器。 # 我猜,肯定有人说你在这写一个变量先赋值一下,然后再把变量返回出去 # 我当然试过了,结果依然是一样的,一样会出现 None return res['data'] else: time.sleep(1) get_http_ip() def start(): info = get_http_ip() print(info) # 这个位置有些时候会打印出 None,这是为啥呢? if __name__ == '__main__': start() 

    修正的代码:

    ip_list = '' def get_http_ip(): global ip_list res = requests.get('http://www.xxx.com') res = json.loads(res.text) if res['code'] == 200: ip_list = res['data'] # 我把结果赋值给全局变量 return res['data'] else: time.sleep(1) get_http_ip() def start(): global ip_list info = get_http_ip() if info == None: # 我这里写一个判断如果返回 None 我就用全局变量赋值 info = ip_list print(info) if __name__ == '__main__': start() 

    那么问题来了,为啥我第一个办法有些时候会返回 None 呢?
    我瞎猜,是不是内存指针跟不上?
    就是前辈们指教一下,什么原因导致的?

    jyyx
        1
    jyyx  
       2020-04-21 16:34:50 +08:00
    递归那里没有返回, else 里面没有 return
    改成 return get_http_ip()
    Virace
        2
    Virace  
       2020-04-21 16:38:24 +08:00 via Android
    马虎
    wzwwzw
        3
    wzwwzw  
       2020-04-21 16:39:46 +08:00
    递归哪里修改成 return get_http_ip()
    smallgoogle
        4
    smallgoogle  
    OP
       2020-04-21 16:41:38 +08:00
    @jyyx @Virace @wzwwzw 沉了 沉了。对不起。对不起。占用资源了。哎。。。
    U7Q5tLAex2FI0o0g
        5
    U7Q5tLAex2FI0o0g  
       2020-04-21 16:42:38 +08:00
    递归,了解一下递归
    答案楼上已经说了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5591 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 06:56 PVG 14:56 LAX 23:56 JFK 02:56
    Do have faith in what you're doing.
    ubao msn 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