Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6076183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:34:43+00:00 2026-05-23T10:34:43+00:00

I have a class that, let’s say, computes a person’s insurance risk, and a

  • 0

I have a class that, let’s say, computes a person’s insurance risk, and a few other variables are computed during computation. I will need access to the risk and the other variables later.

class InsuranceRiskModel:

    self.risk = None
    self.other_var = None
    ...

    def get_risk():
        # do a bunch of calculations, 
        # which in the meantime fills out a bunch of other vars
        self.other_var = 5
        self.risk = 6
        return self.risk

    def get_other_var():
        # risk hasn't been calculated
        if not self.risk:
            raise NotYetCalculatedError("Not yet calculated!")
        return self.other_var

Now in some other function I do:

r = InsuranceRiskModel(person)
risk = r.get_risk()
other_var = r.get_other_var()

Is this a legitimate structure for the sort of program I want? Just throw an exception of the computation hasn’t been run, to prevent getting bogus values?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:34:44+00:00Added an answer on May 23, 2026 at 10:34 am

    No, it is not a legitimate thing to raise a NotYetCalculatedError ever, in any program (unless the calculation would take hours of work).

    get_other_var() should automatically calculate the risk

    What you actually want to do is run all the calculations when you initialize the class, or if you can’t do that, you’ll want to do this:

    class InsuranceRiskModel(object):
        def __init__(self, person):
            self.person = person
            self.calculated = False
    
        def calculateModel(self):
            if not self.calculated:
                self.risk = 6
                self.other_var = 5
                self.calculated = True
    
        @property
        def risk(self):
            self.calculateModel()
            return self.risk
    
        @property
        def other_var(self):
            self.calculateModel()
            return self.other_var
    

    Then you can access InsuranceRiskModel(bob).risk or InsuranceRiskModel(bob).other_var anytime and the calculations will be done transparently.

    To answer your more general question, the “python paradigm for [class attributes represented by] stored calculations” is the class property abstraction, involving transparent user-defined getters and setters and deleters, as demonstrated above; more info here http://docs.python.org/library/functions.html#property

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a class that requires configuration, dependency injection etc. public class
Let's say that I have class , that uses some functionality of dict .
Let's say I have a very simple PrototypeJS class that looks like this: var
Let's say that I have a bunch of class instances that serve different purposes,
Let's say I have a project that is a class library. I have a
Let's say I have a bunch of classes that look something like... class Foo{
Let's say I have class that acts as a smart pointer and releases some
Let's say we have a class that looks like this: class A { public:
Sorry for the bad question title. Let's say that I have DateRange class that
Let's say I have a class that looks something like this: class Foo(Prop1:Int, Prop2:Int,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.