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

如何改变一个方法里的常量

  •  
  •   u2gign · Jul 11, 2022 · 4102 views
    This topic created in 1578 days ago, the information mentioned may be changed or developed.

    一个 config 里的常量,想在执行另一个方法时能改变这个常量,是否可以

    10 replies  •  2022-07-20 09:55:16 +08:00
    kop1989smurf
    kop1989smurf
    Jul 11, 2022
    1
    可以,把这个常量改成变量。
    ethusdt
    ethusdt
    Jul 11, 2022
    2
    都是常量了, 为什么还要改. 直接赋值给一个变量, 改这个变量吧, 以后用这个变量.
    imn1
    imn1
    Jul 11, 2022
    3
    python 几乎没有常量概念,都可以改变

    但注意的是,直接改也很可能不是"临时"的
    想暂时改变的话,最好还是 deepcopy 或者继承到新变量再使用
    darkengine
    darkengine
    Jul 11, 2022
    4
    当一个常量能被改变时,它还是不是常量?
    muchenlou
    muchenlou
    Jul 11, 2022
    5
    Java 一些常量可以是去读取配置文件的。而配置文件的话,是支持修改的。可以参考这个思想?
    llsquaer
    llsquaer
    Jul 11, 2022❤️1
    6
    这问题我遇到过..楼主是想说 config 里面一个 user=111 的值..
    在 a.py 修改为 user=222
    但是在 b.py 里 依然是 user=111

    我的解决变法是 把他改为 字典 user={'k':111}
    这样 2 边都是变化后的值了.
    wuwukai007
    wuwukai007
    Jul 11, 2022
    7
    import config
    config.name = 2
    ojbk
    hoopan
    hoopan
    Jul 12, 2022
    8
    还是定义个变量吧
    u2gign
    u2gign
    OP
    Jul 14, 2022
    9
    @llsquaer 谢谢,是这个意思哈
    wersonliu9527
    wersonliu9527
    Jul 20, 2022
    10
    或许这个 partial 有用
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2473 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 15:13 · PVG 23:13 · LAX 07:13 · JFK 10:13
    ♥ Do have faith in what you're doing.