Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
referencing an attribute
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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')
Skaperen likes this post
Reply
#3
that was what i was thinking i would need to do.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020