Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Capital City by vvm70
class Capital:
_instance = None
def __new__(self, city_name):
if not self._instance:
self._instance = super(Capital, self).__new__(self)
self.city = city_name
return self._instance
def name(self):
return self.city
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
russia_capital_1 = Capital("Moscow")
russia_capital_2 = Capital("London")
russia_capital_3 = Capital("Morocco")
print(russia_capital_3.name())
assert russia_capital_2.name() == "Moscow"
assert russia_capital_3.name() == "Moscow"
assert russia_capital_2 is russia_capital_1
assert russia_capital_3 is russia_capital_1
print("Coding complete? Let's try tests!")
May 17, 2021
Comments: