Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Capital City by hkgok1
class Capital:
_instance = None
_city_name = ''
def __new__(cls, city_name):
if cls._instance is None:
#print('Creating the object')
cls._instance = super(Capital, cls).__new__(cls)
cls._city_name = city_name
return cls._instance
def name(self):
return self._city_name
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
ukraine_capital_1 = Capital("Kyiv")
ukraine_capital_2 = Capital("London")
ukraine_capital_3 = Capital("Marocco")
assert ukraine_capital_2.name() == "Kyiv"
assert ukraine_capital_3.name() == "Kyiv"
assert ukraine_capital_2 is ukraine_capital_1
assert ukraine_capital_3 is ukraine_capital_1
print("Coding complete? Let's try tests!")
March 23, 2022