首页 > 解决方案 > 在函数python中返回值的问题

问题描述

我正在尝试返回当前时间,但由于某种原因我不能。我返回这个:

<function getHorarioActual at 0x000001E292E53950>

我的功能很简单

def getHorarioActual():
    now = datetime.datetime.now()

    horaActual = now.hour
    minutoActual = now.minute
    auxiliar = str(horaActual)+":"+str(minutoActual)
    horarioActual = auxiliar

    return horarioActual

标签: pythondatetime

解决方案


您不需要所有变量来格式化HH:MM. 您的函数可以定义为:

from datetime import datetime

def getHorarioActual():
    return datetime.now().strftime('%H:%M')

这是一个概念证明:

Python 3.7.5 (default, Oct 17 2019, 12:16:48) 
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> def getHorarioActual():
...     return datetime.now().strftime('%H:%M')
... 
>>> getHorarioActual()
'19:05'
>>>

推荐阅读