AttributeError:'module'对象没有属性'strptime'//类? [英] AttributeError: 'module' object has no attribute 'strptime' // class?
本文介绍了AttributeError:'module'对象没有属性'strptime'//类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的交易
类:
):
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
当我试图运行 date
函数时:
tr = Transaction('AAPL',600,'2013-10-25')
pre>
print tr.date
我收到以下错误:
date = datetime.strptime(self.d,%Y-%m-%d)
pre>
AttributeError:'module'对象没有属性'strptime'
如何解决?
解决方案你猜到了:
import datetime
。这意味着您必须这样做:
datetime.datetime.strptime(date,%Y-%m-%d )
以访问
strptime
方法。或者,您可以将import语句更改为:来自datetime import datetime
并且按原样存取。
datetime
模块也命名为classdatetime
:#module类方法
datetime.datetime.strptime(date,%Y-%m-%d)
Here is my
Transaction
class: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
And when I'm trying to run the
date
function:tr = Transaction('AAPL', 600, '2013-10-25') print tr.date
I'm getting the following error:
self.date = datetime.strptime(self.d, "%Y-%m-%d") AttributeError: 'module' object has no attribute 'strptime'
How can I fix that?
解决方案If I had to guess, you did this:
import datetime
at the top of your code. This means that you have to do this:
datetime.datetime.strptime(date, "%Y-%m-%d")
to access the
strptime
method. Or, you could change the import statement to this:from datetime import datetime
and access it as you are.
The people who made the
datetime
module also named their classdatetime
:#module class method datetime.datetime.strptime(date, "%Y-%m-%d")
这篇关于AttributeError:'module'对象没有属性'strptime'//类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文