Problematic Class
Im trying to create a class called Record, though when I try to use it, something goes wrong. Im sure im overlooking something simple. D开发者_如何学Coes anyone mind taking a look?
class Record: def __init__(self, model): self.model= model self.doc_date = [] self.doc_pn = [] print("Record %s has been added.\n") % self def add_doc_date(self, declaration_date): self.doc_date.append(declaration_date) def add_doc_pn(self, declaration_pn): self.doc_pn.append(declaration_pn) def __str__(self): res = "Name: " + self.model + "\n" res = res + "Standard Part Numbers:" + str(self.std_pn) + "\n" res = res + "Declaration Part Numbers" + str(self.doc_pn) + "\n" return res
res = res + "Standard Part Numbers:" + str(self.std_pn) + "\n"
I don't see self.std_pn
defined anywhere.
class Record:
def __init__(self, model):
self.model= model
self.doc_date = []
self.doc_pn = []
self.std_pn = []
print("Record %s has been added.\n") % self
def add_doc_date(self, declaration_date):
self.doc_date.append(declaration_date)
def add_doc_pn(self, declaration_pn):
self.doc_pn.append(declaration_pn)
def __str__(self):
res = "Name: " + self.model + "\n"
res = res + "Doc Date:" + str(self.doc_date) + "\n"
res = res + "Standard Part Numbers:" + str(self.std_pn) + "\n"
res = res + "Declaration Part Numbers" + str(self.doc_pn) + "\n"
return res
>>> t=Record("rec1")
Record Name: rec1
Doc Date:[]
Standard Part Numbers:[]
Declaration Part Numbers[]
has been added.
>>> t.add_doc_date("2010-10-10")
>>> t.add_doc_pn("30")
>>> print t
Name: rec1
Doc Date:['2010-10-10']
Standard Part Numbers:[]
Declaration Part Numbers['30']
>>>
精彩评论