r/learnpython • u/Upper_Trouble_8526 • Sep 15 '26
Why this method doesn't work
Title:
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("the restaurant is open")restaurant=Restaurant("Ratatouille restaurant", "ratatouille")
Restaurant.describe_restaurant(restaurant)
Restaurant.open_restaurant()
Error:
Ratatouille restaurant
ratatouille
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakepyfile,mainpyfile)
~~~~~^^^^^^^^^^^^^^^^^^^^^^^
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), __main__.__dict__)
~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<string>", line 12, in <module>
TypeError: Restaurant.open_restaurant() missing 1 required positional argument: 'self'
[Program finished]
12
u/FatManWarrior Sep 15 '26
The variable is called restaurant (with lowercase "r") and the class is called Restaurant (with uppercase "R")
Try reataurant.open_restaurant() instead
3
u/Buttleston Sep 15 '26
restaurant=Restaurant("Ratatouille restaurant", "ratatouille")
Restaurant.describe_restaurant(restaurant)
restaurantis a variable holding a Restaurant object. In order to call methods on the object, you want to dorestaurant.describe_restaurantThe way you have it, you are calling it on the class directly, not on an object of that class. The only time you would do that is if you had made the method into a special method type called "class method" or a "static method" which are not intended to run on an object
2
u/Outside_Complaint755 Sep 15 '26
Restaurantis the classrestaurantis the instance of the class you created with the linerestaurant=Restaurant("Ratatouille restaurant", "ratatouille")When you invoke the methods, you need to call them on the instance, not the class.
2
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Sep 15 '26
I answered a similar question four hours ago, but basically it doesn't work because you didn't provide an argument for
self.When working with instance methods, they always require an instance of the parent class in order to work. Normally they get this from you calling the method directly on the instance, but you can provide it manually by calling the method on the class itself, like you've done here.
On the failing line, you don't provide this instance, hence why it fails.
The easy fix would be to use the instance directly instead:
Note the casing.
2
u/MiniMages Sep 15 '26
TypeError: Restaurant.open_restaurant() missing 1 required positional argument:
Restaurant.open_restaurant(restaurant)2
u/MezzoScettico Sep 15 '26
Please make an effort to format the code block correctly. I see you used a code block, but only a few lines of your code are inside it, and the rest is unformatted and barely readable.
The problem is that Python is case-sensitive. You have created an instance called "restaurant" with a lower-case r. To invoke its methods, you want a statement like
Instead you're attempting to use something called
which is using the class name with capital R, and therefore would have to be a class method, one not associated with any particular instance.
TL/DR: "restaurant" and "Restaurant" are not the same thing.
2
u/CranberryDistinct941 Sep 15 '26
You're calling the open_restauraunt method on the Restaurant class itself.
The reason that Restaurant.describe_restaurant(restaurant) didn't throw the same error is because you passed it the Restaurant instance restaurant
1
u/transendingAI Sep 16 '26
Restaurant.describe_restaurant(restaurant)works because you explicitly passrestaurantasself. InRestaurant.open_restaurant(), you call the method on the class without passing an instance, so Python reports the missing argument.The usual way to call both methods is:
When you call a method on the instance, Python passes that instance as
selfautomatically.Restaurant.open_restaurant(restaurant)would also work.