0

Python _ Class

Hi! I’m studying ‘class’ in python. There was an example, and I did it exactly same, but it’s not working..... class Restaurant(): def _init_(self, n, t): self.name = n self.type = t def desc_restaurant(self) : print("Name of this restaurant is " + self.name.title()) print("Cusine Type : " + self.type.title()) def open_restaurant(self) : print("The restaurant is open!") res_1 = Restaurant('cafe noma', 'spanish') res_2 = Restaurant('tampopo', 'japanese') It says “Restaurant() takes no arguments” So I modified it to class Restaurant(object) But still not working..... Could you give me some advice?

1st Feb 2019, 1:19 AM
Lucy Lee
Lucy Lee - avatar
4 Antworten
+ 5
And secondly, the "magic methods" of classes have names embedded in 🅓🅞🅤🅑🅛🅔 underscores. You didn't supply __init__ to the class, you used _init_ instead. Which just makes a new method. Not a constructor. And with no constructor provided, Python defaults to the default constructor of the base class of all classes, which takes no arguments. That's how it works.
1st Feb 2019, 1:25 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 4
First and foremost please fix your indentation. This is an appaling visual nightmare.
1st Feb 2019, 1:22 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 2
No problem! Just note this in the future
1st Feb 2019, 4:23 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 1
⏩ Prometheus ⏪ Oh that was it! Thank you so much! I couldn’t tell it was double _ from this text book
1st Feb 2019, 1:58 AM
Lucy Lee
Lucy Lee - avatar