Python Forum
Return Object Created from Multiple Classes - Printable Version

+- Python Forum (
+-- Forum: Python Coding (
+--- Forum: General Coding Help (
+--- Thread: Return Object Created from Multiple Classes (/Thread-Return-Object-Created-from-Multiple-Classes)

Return Object Created from Multiple Classes - emerger - Oct-17-2018

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

RE: Return Object Created from Multiple Classes - ichabod801 - Oct-18-2018

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.

RE: Return Object Created from Multiple Classes - stullis - Oct-18-2018

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.

RE: Return Object Created from Multiple Classes - emerger - Oct-18-2018

So awesome - both responses. Thank you. Tomorrow I will work with this.