这是我的 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
当我尝试运行 date
函数时:
tr = Transaction('AAPL', 600, '2013-10-25')
print tr.date
我收到以下错误:
self.date = datetime.strptime(self.d, "%Y-%m-%d")
AttributeError: 'module' object has no attribute 'strptime'
我该如何解决?
原文由 Michael 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果我不得不猜测,你这样做了:
在你的代码的顶部。这意味着您必须这样做:
访问
strptime
方法。或者,您可以将导入语句更改为:并按原样访问它。
制作
datetime
模块 的人也将他们的 类命名为datetime
: