tags:

views:

96

answers:

2

Im trying to create a class called Record, though when I try to use it, something goes wrong. Im sure im overlooking something simple. Does 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
+7  A: 
res = res + "Standard Part Numbers:" + str(self.std_pn) + "\n"

I don't see self.std_pn defined anywhere.

eumiro
+1  A: 
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']

>>>
amadain
Why have you posted the same answer twice?
katrielalex
sorry I wanted to expand on the code with the method calls showing
amadain
You can edit your previous post by clicking the `edit` link at its bottom left.
katrielalex
@amadain: Not only can you edit your previous answer, you can also delete your previous answer. Since you put in a second answer, I'd suggest you remove your other answer.
S.Lott