Exporting properties to other constructs
When marking an attribute as private in a Python class (e.g., self.__name = "John"), it’s name‑mangled and not directly accessible from other constructs.
To expose such values to other constructs while keeping the attribute encapsulated, it is possible to provide a read-only property using the @property decorator.
class Person(Construct):
def __init__(self, scope: Construct, id: str, *, name: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
self.__name = name
@property
def name(self) -> str:
"""Read-only access for other constructs."""
return self.__nameUsage outside of the construct:
person = Person(self, "Person")
print(person.name) # accesses the exposed property