0

I've "simplified" the ORMs,APIs- lecture 4_web_classes4.py code to try and understand more about Python classes, objects and list. can anyone help me out?

Question 1: Why doesn't print(self.passengers) work like print(list) below? print(self.passengers) returns a memory address instead of a list.

Question 2: how do we print the flight_id? because print(self.passengers.flight_id) doens't seem to work..

>  class Flight:
>     pass
>     counter = 1
> 
>     def __init__(self):
>         self.id = Flight.counter
>         Flight.counter += 1
> 
>         self.passengers = ["self.passangers list 1", "self.passangers list 2"]
> 
> 
>     def add_passenger(self, p):
>         self.passengers.append(p)
>         p.flight_id = self.id
> 
>     def print_info(self):
> 
>         print(self.passengers)
>         # print(self.passengers.flight_id)
> 
>         #Question 1: Why doesn't print(self.passengers) work like print(list) below? print(self.passengers) returns a memory address instead of a list.
>         #Question 2: how do we print the flight_id? because print(self.passengers.flight_id) doens't seem to work..
> 
> #recreating class
> class Passenger:
> 
>     def __init__ (self, name):
>         self.name = name
> 
> #after all definitions, execute the code
> 
> def main():
> 
>     f1 = Flight()
>     michael = Passenger(name = "MichaelScott")      #creates an object under the class Passenger
>     f1.add_passenger(michael)       #calls the function add_passenger(for the flight, f1) passing the value, michael- which is an object under the class Passenger)
>                                     #the function appends michael into the f1.passenger list
>     f1.print_info()                 #prints the attributes for the object f1, namely the list f1.passengers
> 
>     list = [1, 2, 3]
>     print(list)
> 
> 
> 
> 
> if __name__ == "__main__":
>     main()
1

passengers is a list of Passenger objects, therefore print(self.passengers) is printing a list of Passenger objects. This print(self.passengers.flight_id) would fail because self.passengers is a list and the list has no flight_id attribute. Program needs to iterate through passengers list to get the attributes (e.g. flight_id, name) of each Passenger object.

To clarify, passengers is a list of Passenger objects. Each element of the list is a passenger, therefore passengers[i].name and passengers[i].flight_id hold the info you are looking for.

  • Thanks @DinoCoderSaurus. The add_passenger function is suppose to add a flight_id attribute. We've passed 'michael' through it. 2) So how would I go about printing the flight_id (attached to passenger 'michael')? 3) Lastly under what condition does it give a memory address instead of the value? – nvs0000 Mar 17 '20 at 12:37
  • 1
    1) it does 2) michael.flight_id 3) when you print the object instead of the attribute. Clarification (hopefully) added to answer. – DinoCoderSaurus Mar 17 '20 at 12:55
  • That's clear thanks so much! – nvs0000 Mar 18 '20 at 10:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .