Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Capital City by Cyrus490
class Capital:
myname = None
__instance = None
def __new__(cls, n):
if cls.__instance is None:
cls.myname = n
cls.__instance = object.__new__(cls)
return cls.__instance
def __init__(self, city_name):
self.city_name = self.myname
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!")
Oct. 14, 2020