Mic*_*ael 127 python class python-2.7
这是我的Transaction班级:
class Transaction(object):
def __init__(self, company, num, price, date, is_buy):
self.company = company
self.num = num
self.price = price
self.date = datetime.strptime(date, "%Y-%m-%d")
self.is_buy = is_buy
Run Code Online (Sandbox Code Playgroud)
当我试图运行该date功能时:
tr = Transaction('AAPL', 600, '2013-10-25')
print tr.date
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
self.date = datetime.strptime(self.d, "%Y-%m-%d")
AttributeError: 'module' object has no attribute 'strptime'
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
iCo*_*dez 317
如果我不得不猜测,你这样做了:
import datetime
Run Code Online (Sandbox Code Playgroud)
在代码的顶部.这意味着你必须这样做:
datetime.datetime.strptime(date, "%Y-%m-%d")
Run Code Online (Sandbox Code Playgroud)
访问该strptime方法.或者,您可以将import语句更改为:
from datetime import datetime
Run Code Online (Sandbox Code Playgroud)
并按原样访问它.
制作datetime模块的人也将他们的班级datetime命名为:
#module class method
datetime.datetime.strptime(date, "%Y-%m-%d")
Run Code Online (Sandbox Code Playgroud)
Tho*_*zco 15
使用正确的呼叫:strptime是的类方法datetime.datetime的类,它不是一个功能datetime模块.
self.date = datetime.datetime.strptime(self.d, "%Y-%m-%d")
Run Code Online (Sandbox Code Playgroud)
正如在评论中提到的乔恩·克莱门茨,有些人做from datetime import datetime,这将绑定datetime名称到datetime类,并让您最初的代码工作.
要确定你面对(在未来)这种情况下,看看你的import语句
import datetime:那就是模块(这就是你现在所拥有的).from datetime import datetime:那是班级.小智 5
我也遇到了同样的问题,但不是你说的解决方案。所以我将“从日期时间导入日期时间”更改为“导入日期时间”。之后,在“datetime.datetime”的帮助下,我可以正确获取整个模块。我想这就是这个问题的正确答案。
| 归档时间: |
|
| 查看次数: |
162155 次 |
| 最近记录: |