Python Forum

Full Version: referencing an attribute
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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')
that was what i was thinking i would need to do.