4 Replies - 281 Views - Last Post: 14 February 2013 - 04:16 PM Rate Topic: -----

#1 Static Hazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 25-November 09

Passing an object as a paramter

Posted 14 February 2013 - 03:54 PM

I have two classes, called Journal and JournalEntry. The JournalEntry constructor has several parameters that are passed with it is instantiated, like time, date, subject, context. In the Journal class, there is a function called addEntry(self, JournalEntry) that adds an entry to the journal. I'm having trouble getting the addEntry function started...

My problem is that I don't really know how to instantiate the entry...I'm not used to dynamically typed languages like Python.

The actual values are supplied in the JournalEntry class. For example, an entry could be entry1 = journalentry.JournalEntry("1215", "02142013", "My day", "Today was Valentine's Day.").

Class Journal(object):
    def __init__(self):
        journalList = []

    def addEntry(self, entry):
        self.theEntry = entry #is this right?
        self.theEntry = entry(time, date, subject, context) #or is this?
        journalList += entry



The Journal constructor creates an empty list, and then addEntry would add an entry to the list each time. Will this work?

Is This A Good Question/Topic? 0
  • +

Replies To: Passing an object as a paramter

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,686
  • Joined: 13-March 10

Re: Passing an object as a paramter

Posted 14 February 2013 - 04:01 PM

Well, your init() method is wrong. You need to create an instance variable that will keep track of journal entries so it should be:
self.journalList = []
.

Then, you can create the addEntry() method and simply append it.
def addEntry(self, entry):
     self.journalList.append(entry)


Note that addEntry takes as parameter already instantiated Entry object.
Was This Post Helpful? 1
  • +
  • -

#3 woooee  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 78
  • Joined: 21-November 12

Re: Passing an object as a paramter

Posted 14 February 2013 - 04:04 PM

I am assuming that you want to keep each journal entry so each instance is appended to self.journal_list in the code below. It would be simpler to use a list of lists or a dictionary with list to keep track of the data. Also, code is easier for others to read when it conforms to the Python Style Guide
class Journal(object):
    def __init__(self):
        self.journalList = []

    def addEntry(self):
        the_entry = JournalEntryClass(time, date, subject, context) #or is this?
        self.journalList.append(the_entry)

    def print_all_entries(self):
        for instance in self.journalList:
            print instance.time, instance.date, instance.subject, instance.context 

This post has been edited by woooee: 14 February 2013 - 04:11 PM

Was This Post Helpful? 1
  • +
  • -

#4 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,686
  • Joined: 13-March 10

Re: Passing an object as a paramter

Posted 14 February 2013 - 04:08 PM

View Postwoooee, on 14 February 2013 - 04:04 PM, said:

def print_all_entries(self):
for instance in self.journalList():
print instance.time, instance.date, instance.subject, instance.context


self.journalList <-- without ().

And CLASS should be spelt with lower case letters class not Class.
Was This Post Helpful? 0
  • +
  • -

#5 woooee  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 78
  • Joined: 21-November 12

Re: Passing an object as a paramter

Posted 14 February 2013 - 04:16 PM

Corrected. Didn't catch the capitalized Class in the original code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1