Here is my Transaction
class:
这是我的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
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?
我该如何解决这个问题?
2 个解决方案
#1
207
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:
访问strptime方法。或者,您可以将import语句更改为:
from datetime import datetime
and access it as you are.
并按原样访问它。
The people who made the datetime
module also named their class datetime
:
制作datetime模块的人也将他们的类命名为datetime:
#module class method
datetime.datetime.strptime(date, "%Y-%m-%d")
#2
12
Use the correct call: strptime
is a classmethod of the datetime.datetime
class, it's not a function in the datetime
module.
使用正确的调用:strptime是datetime.datetime类的类方法,它不是datetime模块中的函数。
self.date = datetime.datetime.strptime(self.d, "%Y-%m-%d")
As mentioned by Jon Clements in the comments, some people do from datetime import datetime
, which would bind the datetime
name to the datetime
class, and make your initial code work.
正如Jon Clements在评论中所提到的,有些人从datetime import datetime开始,它将datetime名称绑定到datetime类,并使初始代码工作。
To identify which case you're facing (in the future), look at your import statements
要确定您面临的案例(将来),请查看您的import语句
-
import datetime
: that's the module (that's what you have right now). - import datetime:这是模块(这就是你现在拥有的)。
-
from datetime import datetime
: that's the class. - 从datetime import datetime:这是类。
#1
207
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:
访问strptime方法。或者,您可以将import语句更改为:
from datetime import datetime
and access it as you are.
并按原样访问它。
The people who made the datetime
module also named their class datetime
:
制作datetime模块的人也将他们的类命名为datetime:
#module class method
datetime.datetime.strptime(date, "%Y-%m-%d")
#2
12
Use the correct call: strptime
is a classmethod of the datetime.datetime
class, it's not a function in the datetime
module.
使用正确的调用:strptime是datetime.datetime类的类方法,它不是datetime模块中的函数。
self.date = datetime.datetime.strptime(self.d, "%Y-%m-%d")
As mentioned by Jon Clements in the comments, some people do from datetime import datetime
, which would bind the datetime
name to the datetime
class, and make your initial code work.
正如Jon Clements在评论中所提到的,有些人从datetime import datetime开始,它将datetime名称绑定到datetime类,并使初始代码工作。
To identify which case you're facing (in the future), look at your import statements
要确定您面临的案例(将来),请查看您的import语句
-
import datetime
: that's the module (that's what you have right now). - import datetime:这是模块(这就是你现在拥有的)。
-
from datetime import datetime
: that's the class. - 从datetime import datetime:这是类。