我有这三个类
import datetime
class MetalType(object):
Silver, Gold, Platinum = range(0, 3)
class Metals(object):
def __init__(self,pType,pVariant):
self.type= pType
self.variant=pVariant
class Transaction(object):
id=0
def __init__(self,pMetal,pQuantity,pTransType,pPrice):
Transaction.id+=1
self.transdate= datetime.date.today()
self.metal= pMetal
self.quantity =pQuantity
self.TransType =pTransType
self.price =pPrice
现在我想创建一个Transaction对象并打印类型
Silver=Metals(MetalType.Silver,"Heraus")
Transaction1=Transaction(Metals,100,"buy",100.00)
print(Transaction1.metal.type)
但是我收到以下错误
AttributeError: type object 'Metals' has no attribute 'type'
我在这里做错了什么?
答案 0 :(得分:0)
Transaction1=Transaction(Metals,100,"buy",100.00)
应该成为
Transaction1=Transaction(Silver,100,"buy",100.00)
另外,一个友好的建议:不要使用大写的变量名,以免将它们与类名混淆。你选择的资本化使得这比现在更难以发现。