corelp.prop module
- corelp.prop(*, cache=False, variable=False, link=None)[source]
This function serves as an improved property decorator. By default, calls the function as a normal property. However if the readonly attribute of same name (starting with “_”) exists and is not None, it returns this value. This function can also be used to link the property to another object attribute, then the return value should be this linked name.
- Parameters:
cache (bool) – True to set readonly attribute at first call.
variable (bool) – True to create a getter that will always use the _attr as a variable.
link (bool) – True to link property to another object attribute.
- Returns:
decorator – This is the decorator to apply.
- Return type:
property
Examples
>>> from corelp import prop ... >>> class MyClass : ... ... # Set default value ... @prop() ... def defaultattr(self) : ... return "MyDefaultValue" # --> is overriden if "_defaultattr" exists ... ... # Set initialization value ... @prop(cache=True) ... def cachedattr(self) : ... return "MyCachedValue" # --> called once and cached in "_cachedattr" ... ... ... >>> instance = MyClass() # Creates instance of MyClass >>> class MyTwin : ... ... # The following properties do the same thing ... ... mytwin = instance ... # Links on attribute name ... @prop(link="mytwin") # --> links to self.mytwin ... def attrlink(self) : ... return "defaultattr" # --> calls the "defaultattr" attribute of the linked object ... ... # Links on object ... @prop(link=instance) # --> links to an object ... def objectlink(self) : ... return "defaultattr" # --> calls the "defaultattr" attribute of the linked object