问题类

时间:2010-09-22 12:42:23

标签: python class

我试图创建一个名为Record的类,但是当我尝试使用它时,出现问题。我肯定会忽略一些简单的事情。有人介意看看吗?

      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

2 个答案:

答案 0 :(得分:6)

res = res + "Standard Part Numbers:" + str(self.std_pn) + "\n"

我没有在任何地方看到self.std_pn

答案 1 :(得分:0)

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']

>>>