Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Return Object Created from Multiple Classes
I have an object (lyst2) that is created from multiple classes. When I use that returned object in another module and iterate through it, I get the addresses of the object's values and not the values themselves. Without having to display my entire program here, can anyone tell me why addresses might be written to my text file instead of the values?
(Note: Let me know if I need to display my entire code).
Here is the module that is using the returned object lyst2:
from movie import Movie
from rental import Rental
from customer import Customer
import enter_data
def createTextFile(lyst2): 
    col_format = '\n' + "{:<15}"*5 + '\n'
    col_format2 = "{:<15}"*1+'\n'
    text_file = open("RedBox.txt",'a')
    text_file.write(col_format.format("Customer Name","Movie Title","Days Rented","Type Movie","Cost"))
    for i in lyst2:
        i = str(i)
Here is one of my classes:

class Movie(object):
    def __init__(self, children, regular, new_release, cost):
        self.children = children
        self.regular = regular
        self.new_release = new_release
        self.cost = cost
    def __str__(self):
        return str(self.children) + str(self.regular) + str(self.new_release + str(self.cost))
Here is the module that populates lyst2:
from movie import Movie
from rental import Rental
from customer import Customer
# Import date time module

def enterData():
    costLyst = []
    lyst2 = []
    continue_on = 'y'
    while continue_on == 'y':
        custName = input("Enter the name of the customer: ")
        custName = custName.capitalize()
        custName = Customer(custName)

        rentTime = int(input("Enter the number of days rented: "))
        movieName = input ("Enter the name of the movie: ")
        movieName = movieName.capitalize()
        rentalData1 = Rental(movieName,"")
        rentalData2 = Rental("",rentTime)
        movieMenu = int(input("Enter the movie type: \n"
                          "Enter 1 for Children's movie.\n"
                          "Enter 2 for Regular movie.\n"
                          "Enter 3 for New Release movie.\n"))
        if movieMenu == 1:
            children = "Children's"
            regular = ''
            new_release = ''
            cost = float(.55)
            movieType1 = Movie("Children",'','','')
            movieType2 = Movie('','','',.55)
            c_cost = movieType2.cost
        if movieMenu == 2:
            children = ""
            regular = 'Regular'
            new_release = ''
            cost = 22
            movieType = Movie(children,regular,new_release,cost)
        if movieMenu == 3:
            children = ""
            regular = ''
            new_release = 'New-Release'
            movieType = Movie(children,regular,new_release)
        continue_on = input("Would you like to enter more data for "+str(custName)+"?  y or n ")
        if continue_on == 'y':
    return lyst2,costLyst
Here is the data written to the text file. They are the addresses instead of the values:

Customer Name Movie Title Days Rented Type Movie Cost
[<customer.Customer object at 0x10b7fd0b8>, <rental.Rental object at 0x10b7d6f28>, <rental.Rental object at 0x10b7fd5f8>, <movie.Movie object at 0x10b7cab00>, <movie.Movie object at 0x10b7cac88>]
That's the default text representation of objects. You get that for any object you create unless you override the __repr__ method. However, the list actually has the addresses (or rather, pointers to those addresses). Typical user created classes (that is, sub-classes of object) are mutable. So they are passed around by reference, not value. So say you have lyst1 and lyst2, and the Movie object for The Outlaw Josie Wales is in both lists. If you change the one in lyst1, that will change the one in lyst2, since both lists just have references to the same object.
emerger likes this post
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

I do believe the problem is that your objects (Customer, Rental, etc.) do not have a the methods __repr__() or __str__() defined. Those methods are called when printing objects.
emerger likes this post
So awesome - both responses. Thank you. Tomorrow I will work with this.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Return Multiple or one just JohnnyCoffee 14 602 May-01-2020, 11:26 AM
Last Post: JohnnyCoffee
  How to mock an object that is created during function call? Schlangenversteher 0 254 Jan-31-2020, 01:36 PM
Last Post: Schlangenversteher
  connecting to the spreadsheet object inside multiple enterprise software apps ajetrumpet 0 240 Jan-19-2020, 03:36 PM
Last Post: ajetrumpet
  Save all values to pandas of multiple classes jenniferruurs 0 282 Sep-13-2019, 12:10 PM
Last Post: jenniferruurs
  How to sharing object between multiple process from main process using Pipe Subrata 1 839 Sep-03-2019, 09:49 PM
Last Post: woooee
  Return a value when I equal an object from a class ihouses 4 489 Jul-10-2019, 02:44 AM
Last Post: SheeppOSU
  Multiple "return" statements or "result" variable? WolfWayfarer 7 2,599 Jul-25-2018, 07:51 AM
Last Post: perfringo
  Using classes? Can I just use classes to structure code? muteboy 5 1,824 Nov-01-2017, 04:20 PM
Last Post: metulburr
  Accept Multiple Lines of Input into a User Created File Bragger 0 1,163 Oct-29-2017, 04:15 PM
Last Post: Bragger
  Multiple classes in another class Dargonoth 3 1,216 Sep-08-2017, 12:29 PM
Last Post: Dargonoth

Forum Jump:

Users browsing this thread: 1 Guest(s)