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

Python 如何设置一个随着系统时间变化的动态变量?

  •  
  •   wsds · Apr 26, 2018 · 5410 views
    This topic created in 3123 days ago, the information mentioned may be changed or developed.

    想取当前时间,但取时间那一坨内容太长了,复制的到处都是,封装一个方法行是行,但感觉有点啰嗦 我是想能不能通过一个动态变量 a 来获取当前时间,能做到吗?直接赋值,取的就是变量生成那一瞬间的固定时间了,不是我想要的,我想要的是 print(a) 就能获取当前时间

    >>> a =  time.strftime('%Y-%m-%d %H:%M:%S')
    >>> a
    '2018-04-26 14:16:17'
    >>> a
    '2018-04-26 14:16:17'
    >>> a
    '2018-04-26 14:16:17'
    >>>
    
    

    ==================

    >>> def a():
    ...     return time.strftime('%Y-%m-%d %H:%M:%S')
    ...
    >>> a()
    '2018-04-26 14:19:36'
    >>> a()
    '2018-04-26 14:19:39'
    >>> a()
    '2018-04-26 14:19:41'
    >>>
    
    
    9 replies  •  2018-04-26 15:40:34 +08:00
    Contextualist
    Contextualist
    Apr 26, 2018 via iPad
    1
    既然要 class 的 repr 为函数的结果,定义 metaclass 的 __repr__
    ``` python 3
    class _a(type):
       def __repr__(self):
          return time.strftime('%Y-%m-%d %H:%M:%S')

    class a(object, metaclass = _a):
       pass

    print(a)
    ```
    wsds
    wsds
    OP
    Apr 26, 2018
    2
    @Contextualist /惊讶
    knightdf
    knightdf
    Apr 26, 2018
    3
    ``` python
    class A(object):
    @property
    def a(self):
    return time.strftime('%Y-%m-%d %H:%M:%S')
    ```

    print(A().a)
    wsds
    wsds
    OP
    Apr 26, 2018
    4
    @knightdf 比直接定义方法还麻烦啊
    goreliu
    goreliu
    Apr 26, 2018
    5
    你是嫌定义麻烦还是调用麻烦?定义麻烦的话:

    a = lambda:time.strftime('%Y-%m-%d %H:%M:%S')

    应该没有更简单的办法了。调用麻烦的话,就一楼。
    shintendo
    shintendo
    Apr 26, 2018
    6
    你在提出这个需求的时候,有没有想过这种动态变量,假如有的话,底层要以什么原理来实现?
    每隔一个毫秒,系统自动更新一次这个变量所在内存里的值吗?
    还是平时不变,只在你取值的时候更新一下?
    这后者不就是函数么,还是说你只是想要个省略括号的语法糖?
    Shazoo
    Shazoo
    Apr 26, 2018
    7
    ```python
    >>> a = lambda :time.strftime('%Y-%m-%d %H:%M:%S')
    >>> a()
    '2018-04-26 15:32:20'
    >>> a()
    '2018-04-26 15:32:23'
    >>>
    ```
    lauix
    lauix
    Apr 26, 2018
    8
    创建动态变量

    locals()[变量名] = 变量值
    est
    est
    Apr 26, 2018❤️3
    9
    import time

    now=type('now', (), {'__repr__': lambda s: str(time.time())})()



    使用:

    In [105]: now
    Out[105]: 1524728419.898981

    In [106]: now
    Out[106]: 1524728420.5150971

    In [107]: now
    Out[107]: 1524728421.219552

    In [108]: now
    Out[108]: 1524728425.09097
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2106 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 11:38 · PVG 19:38 · LAX 03:38 · JFK 06:38
    ♥ Do have faith in what you're doing.