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

django views.py 中, datetime.datetime.now() 作为默认参数时值不会改变

  •  
  •   cheerzeng · 2017-02-06 11:48:33 +08:00 · 2344 次点击
    这是一个创建于 2634 天前的主题,其中的信息可能已经有所发展或是发生改变。

    hi , 如题, 不知道这是不是 django 的特性?直接用 python 试了没这效果。

    import datetime
    def test(a, b=datetime.datetime.now()):
        print a
        print "b %s" %b
        print "c %s" %datetime.datetime.now()
    
    test(2)
    

    输出结果 b 的值不会改变, c 的正常。

    14 条回复    2017-02-07 20:26:10 +08:00
    111111111111
        1
    111111111111  
       2017-02-06 11:55:50 +08:00
    In [1]: import datetime

    In [2]: def test(a, b=datetime.datetime.now()):
    ...: print a
    ...: print "b %s" %b
    ...: print "c %s" %datetime.datetime.now()
    ...:


    In [3]: import time

    In [4]: for i in range(6):
    ...: time.sleep( 2)
    ...: test(i)
    ...:
    0
    b 2017-02-06 11:53:42.707000
    c 2017-02-06 11:55:00.035000
    1
    b 2017-02-06 11:53:42.707000
    c 2017-02-06 11:55:02.039000
    2
    b 2017-02-06 11:53:42.707000
    c 2017-02-06 11:55:04.044000
    3
    b 2017-02-06 11:53:42.707000
    c 2017-02-06 11:55:06.049000
    111111111111
        3
    111111111111  
       2017-02-06 11:59:10 +08:00
    @111111111111 打开之后搜索“原因解释如下:”
    jingniao
        4
    jingniao  
       2017-02-06 12:00:13 +08:00
    年前刚遇到这个问题,你把括号去掉就好了
    pango
        5
    pango  
       2017-02-06 12:01:57 +08:00
    请使用:
    b=lambda: datetime.datetime.now()

    建议先把书看看好,基础概念弄清楚后可以节省很多时间
    eyeKi1l
        6
    eyeKi1l  
       2017-02-06 12:14:47 +08:00 via Android
    默认参数的默认值何时被计算?
    cxh116
        7
    cxh116  
       2017-02-06 12:25:24 +08:00
    def test(a, b=None)):
    if b is None:
    b = datetime.datetime.now()

    易懂的办法
    cheerzeng
        8
    cheerzeng  
    OP
       2017-02-06 13:42:14 +08:00
    @111111111111 @cxh116 @eyeKi1l @jingniao @pango 谢谢各位,基础功也确实欠缺。
    julyclyde
        9
    julyclyde  
       2017-02-06 14:58:47 +08:00
    @pango lambda 原来是这么用的!学习了!
    julyclyde
        10
    julyclyde  
       2017-02-06 14:59:09 +08:00
    @eyeKi1l 默认参数的默认值被计算的时间,是“运行该函数声明行”的时间
    eyeKi1l
        11
    eyeKi1l  
       2017-02-06 19:08:18 +08:00 via Android
    @julyclyde 我在质问楼主谢谢
    julyclyde
        12
    julyclyde  
       2017-02-06 22:01:08 +08:00
    @eyeKi1l 那你应该用感叹号啊
    twoyuan
        13
    twoyuan  
       2017-02-07 09:08:08 +08:00
    话说这边使用 lambda 和直接传 b=datetime.datetime.now 一样吧
    ibigbug
        14
    ibigbug  
       2017-02-07 20:26:10 +08:00
    @julyclyde 只是一种提示的语气了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5426 人在线   最高记录 6543   ·     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
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.