Python Forum
Return Object Created from Multiple Classes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return Object Created from Multiple Classes
#1
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]
Reply


Messages In This Thread
Return Object Created from Multiple Classes - by emerger - Oct-17-2018, 11:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  This result object does not return rows. It has been closed automatically dawid294 6 1,081 Mar-30-2024, 03:08 AM
Last Post: NolaCuriel
  how to return a reference to an object? Skaperen 8 1,221 Jun-07-2023, 05:30 PM
Last Post: Skaperen
  super multiple parallel classes catlessness 2 1,339 Jun-07-2022, 02:35 PM
Last Post: deanhystad
  please help with classes and return values jamie_01 5 1,804 Jan-17-2022, 02:11 AM
Last Post: menator01
  Function - Return multiple values tester_V 10 4,468 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Object inheriting from two classes? Melendroach 3 2,172 May-11-2021, 06:37 PM
Last Post: deanhystad
  Object already contains values when created Stef 4 2,399 Aug-20-2020, 09:42 AM
Last Post: Stef
  isinstance() always return true for object type check Yoki91 2 2,575 Jul-22-2020, 06:52 PM
Last Post: Yoki91
  Return Multiple or one just JohnnyCoffee 14 5,310 May-01-2020, 11:26 AM
Last Post: JohnnyCoffee
  How to mock an object that is created during function call? Schlangenversteher 0 1,980 Jan-31-2020, 01:36 PM
Last Post: Schlangenversteher

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020