Python Forum
Return Object Created from Multiple Classes - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: General Coding Help (https://python-forum.io/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

Hello.
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)
        text_file.write(col_format2.format(i))
        print(i)
    
    text_file.close()
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)
        lyst2.append(custName)

        
        rentTime = int(input("Enter the number of days rented: "))
        movieName = input ("Enter the name of the movie: ")
        movieName = movieName.capitalize()
        rentalData1 = Rental(movieName,"")
        lyst2.append(rentalData1)
        rentalData2 = Rental("",rentTime)
        lyst2.append(rentalData2)
        
        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)
            lyst2.append(movieType1)
            c_cost = movieType2.cost
            lyst2.append(movieType2)
            costLyst.append(c_cost)
            
        if movieMenu == 2:
            children = ""
            regular = 'Regular'
            new_release = ''
            cost = 22
            movieType = Movie(children,regular,new_release,cost)
            costLyst.append(movieType)
        
        if movieMenu == 3:
            children = ""
            regular = ''
            new_release = 'New-Release'
            movieType = Movie(children,regular,new_release)
            lyst2.append(movieType)
        continue_on = input("Would you like to enter more data for "+str(custName)+"?  y or n ")
        if continue_on == 'y':
            continue
        else:
            pass
   
    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>]
[0.55]


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.
Pete