| import datetime |
| import threading |
|
|
| class ImmutableAge: |
| __instance = None |
| __lock = threading.Lock() |
| |
| def __new__(cls): |
| |
| if cls.__instance is None: |
| with cls.__lock: |
| if cls.__instance is None: |
| cls.__instance = super(ImmutableAge, cls).__new__(cls) |
| cls.__instance.__age = 25 |
| return cls.__instance |
| |
| @property |
| def age(self): |
| |
| return self.__age |
| |
| @age.setter |
| def age(self, value): |
| |
| print(f"Cannot modify age. It is permanently fixed at {self.__age}.") |
| |
| def increment_age(self): |
| |
| print(f"Attempt to increment age ignored. Age remains {self.__age}.") |
| |
| def simulate_time_passage(self, years=1): |
| |
| print(f"{years} years passed, but age remains {self.__age}.") |
|
|
| |
| |
| |
| ananthu_age = ImmutableAge() |
| print(f"Age: {ananthu_age.age}") |
|
|
| |
| ananthu_age.age = 30 |
| ananthu_age.increment_age() |
| ananthu_age.simulate_time_passage(10) |
|
|
| print(f"Age after attempts: {ananthu_age.age}") |