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

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

  •  
  •   wsds · 2018-04-26 14:24:17 +08:00 · 4632 次点击
    这是一个创建于 2186 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想取当前时间,但取时间那一坨内容太长了,复制的到处都是,封装一个方法行是行,但感觉有点啰嗦 我是想能不能通过一个动态变量 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 条回复    2018-04-26 15:40:34 +08:00
    Contextualist
        1
    Contextualist  
       2018-04-26 14:56:11 +08:00 via iPad
    既然要 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
        2
    wsds  
    OP
       2018-04-26 15:04:22 +08:00
    @Contextualist /惊讶
    knightdf
        3
    knightdf  
       2018-04-26 15:16:15 +08:00
    ``` python
    class A(object):
    @property
    def a(self):
    return time.strftime('%Y-%m-%d %H:%M:%S')
    ```

    print(A().a)
    wsds
        4
    wsds  
    OP
       2018-04-26 15:20:00 +08:00
    @knightdf 比直接定义方法还麻烦啊
    goreliu
        5
    goreliu  
       2018-04-26 15:27:44 +08:00
    你是嫌定义麻烦还是调用麻烦?定义麻烦的话:

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

    应该没有更简单的办法了。调用麻烦的话,就一楼。
    shintendo
        6
    shintendo  
       2018-04-26 15:28:53 +08:00
    你在提出这个需求的时候,有没有想过这种动态变量,假如有的话,底层要以什么原理来实现?
    每隔一个毫秒,系统自动更新一次这个变量所在内存里的值吗?
    还是平时不变,只在你取值的时候更新一下?
    这后者不就是函数么,还是说你只是想要个省略括号的语法糖?
    Shazoo
        7
    Shazoo  
       2018-04-26 15:33:12 +08:00
    ```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
        8
    lauix  
       2018-04-26 15:38:39 +08:00
    创建动态变量

    locals()[变量名] = 变量值
    est
        9
    est  
       2018-04-26 15:40:34 +08:00   ❤️ 3
    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
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2853 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:06 · PVG 21:06 · LAX 06:06 · JFK 09:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.