I just started learning classes in Python and faced such a problem (or not) that when outputting the result, the first line goes:

<__main__.Restaurant object at 0x0042DE90> 

I was interested in this, but I did not find the answer on the Internet, so I hope that the experts will help. Here is all the code to clarify the issue:

 class Restaurant: def __init__(self, restaurant_name, cuisine_type): self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type def describe_restaurant(self): print(self.restaurant_name) print(self.cuisine_type) def open_restaurant(self): print(self.restaurant_name+' is open!') my_rest = Restaurant('Clot monet', 'classical') print(my_rest) my_rest.describe_restaurant() my_rest.open_restaurant() 

OUTPUT:

 <__main__.Restaurant object at 0x0042DE90> Clot monet classical Clot monet is open! Process finished with exit code 0 
  • 3
    Because any file directly started by python is a __main__ module, and the Restaurant class is created inside the __main__ - andreymal module 6:29 pm
  • @andreymal, do not quite understand what the error is, you can "chew"?) Or rather, how to remove this line by correcting the code itself - Arsen_Aganesov
  • one
    What kind of error is it? Everything is in order here, there are no problems - andreymal pm
  • @Arsen_Aganesov, why did you decide that this is a mistake? This is a description of the object of your class. If you want to show yours, then redefine the __str__ method in your class, example: github.com/gil9red/SimplePyScripts/blob/… , github.com/gil9red/SimplePyScripts/blob/… - gil9red

1 answer 1

This is the standard repr representation of an object inherited from object.

How to fix - you can add your class repr-description instead of the standard one:

 class Restaurant: def __init__(self, restaurant_name, cuisine_type): self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type def describe_restaurant(self): print(self.restaurant_name) print(self.cuisine_type) def open_restaurant(self): print(self.restaurant_name+' is open!') def __repr__(self): return '{}(restaurant_name={!r}, cuisine_type={!r})'.format(self.__class__.__name__, self.restaurant_name, self.cuisine_type) my_rest = Restaurant('Clot monet', 'classical') print(my_rest) my_rest.describe_restaurant() my_rest.open_restaurant() 

Conclusion:

 Restaurant(restaurant_name='Clot monet', cuisine_type='classical') Clot monet classical Clot monet is open!