【问题标题】:datetime.today attributes errordatetime.today 属性错误
【发布时间】:2013-07-23 19:42:00
【问题描述】:

我正在尝试使用Datetime.today 代码倒计时秒,但每次我调用时间属性时,解释器都会否认该属性存在。例如:

x=datetime.today()
x= x.hour
print(x)

将返回:

Traceback (most recent call last):
File "C:\Users\manuel\Downloads\graphics master v1.py", line 2, in <module>
x=x.hour
AttributeError: 'builtin_function_or_method' object has no attribute 'hour'

在其他程序中,我对此进行了测试,以确保这是正确的属性和语法,但在我的主程序中,我不断收到此错误。

【问题讨论】:

  • 您的示例代码运行良好,您是否在不更新回溯的情况下更新了它?
  • @RonaldOussoren:原来写的时候没有括号。
  • 这应该可以解决问题。您是否在“x=x.hour”之前添加了一条打印语句来检查“x”是否具有您期望的类型?

标签: datetime python-3.x


【解决方案1】:

datetime.today 是一个方法而不是一个属性。

试试 datetime.today()

【讨论】:

  • 我试过了,但没有用。如果这是问题所在,则错误将在上一行。
  • @user2611919:The parentheses () should help。再次检查。
  • @user2611919:错误不会出现在前一行。 x 将等于方法。所以你可以先做 x=datetime.today,然后再做 x()。基本上 x 将存储 today 方法本身而不是返回的类。
猜你喜欢
  • 1970-01-01
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-15
  • 2018-05-01
相关资源
最近更新 更多