推荐学习书目
› 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
frmongo
V2EX  ›  Python

设置多个 global 变量有没有方便的操作?

  •  
  •   frmongo · Oct 19, 2020 · 2955 views
    This topic created in 2207 days ago, the information mentioned may be changed or developed.

    语言 python3, 比如我要设置 50 个变量为全局变量,都是类似这句话

    global para_1 
    

    除了写 50 行声明语句,有用几行就能实现的写法吗?

    11 replies  •  2020-10-19 17:41:40 +08:00
    frmongo
    frmongo
    OP
    Oct 19, 2020
    1
    我找到一种稍微简便的写法

    global para_1,para_2...para_50

    还有别的方法吗
    kiracyan
    kiracyan
    Oct 19, 2020
    2
    用脚本写
    BingoXuan
    BingoXuan
    Oct 19, 2020 via Android
    3
    注入 globals 的字典
    frmongo
    frmongo
    OP
    Oct 19, 2020
    4
    @BingoXuan cool
    zxCoder
    zxCoder
    Oct 19, 2020
    5
    数组
    laike9m
    laike9m
    Oct 19, 2020❤️1
    6
    > 我要设置 50 个变量为全局变量

    那你可能需要重构一下代码了
    JB18CM
    JB18CM
    Oct 19, 2020
    7
    切片
    yucongo
    yucongo
    Oct 19, 2020
    8
    p_list = [f'para_{idx}' for idx in range(1, 51)]
    globals().update(dict(zip(p_list, [None] * 50)))
    typetraits
    typetraits
    Oct 19, 2020❤️1
    9
    50 个?不如直接存入一个 dict,然后把这个 dict 放到 globals 里
    faketemp
    faketemp
    Oct 19, 2020
    10
    只有我一个人把标题看成"golang 变量"了吗
    frmongo
    frmongo
    OP
    Oct 19, 2020
    11
    @typetraits 嗯,多谢,我现在就这些写的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3334 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 301ms · UTC 10:37 · PVG 18:37 · LAX 02:37 · JFK 05:37
    ♥ Do have faith in what you're doing.