Python Forum
referencing an attribute - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: referencing an attribute (/thread-40995.html)



referencing an attribute - Skaperen - Oct-26-2023

is there a way to make a variable be a reference to an attribute? i would like to set up a named reference to decimal.getcontext().prec so i have a simpler way to set the decimal precision. this aappears to be an attribute.


RE: referencing an attribute - snippsat - Oct-27-2023

Cannot directly make a variable a reference to an attribute,
but can create a wrapper function that allows to set the value of the attribute more easily.
import decimal

def pre(prec=None):
    if prec is not None:
        decimal.getcontext().prec = prec
    return decimal.getcontext().prec
>>> pre()
28
>>> n = decimal.Decimal(2).sqrt()
>>> n
Decimal('1.414213562373095048801688724')
>>> pre(5)
5
>>> n = decimal.Decimal(2).sqrt()
>>> n
Decimal('1.4142')



RE: referencing an attribute - Skaperen - Oct-28-2023

that was what i was thinking i would need to do.