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 7821049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:31:21+00:00 2026-06-02T07:31:21+00:00

Let’s say I’ve got a variable A that is the result of a function/expression

  • 0

Let’s say I’ve got a variable A that is the result of a function/expression F. F in it’s turn has a number of other variables in it, let’s say X,Y and Z.

Is it possible to bind A to F so that whenever X,Y or Z changes, A will be updated automatically?

What I want to avoid is that everytime X,Y and Z changes, I have to remember to update A explicitly in the code. I also don’t want to call the function everytime I want to use the A.

Example (as per requested): I’ve got the following function:

def calcHits():
    return sum(hitDiceRolls,level*modList['con']) 

and in my program (outside of the function), I’ve got a variable called hitPoints (yes, it’s a roleplaying game program). Whenever the variables that’s used in the function is changed, I want hitPoints to change as well.

  • 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-06-02T07:31:23+00:00Added an answer on June 2, 2026 at 7:31 am

    There is no way in Python to automatically rebind a name in global or local scope in response to other names being rebound. However, it should be possible to make a class that can keep track of some values and have a member function that returns the value you called A. And, as @Alok pointed out, you can use property descriptors to make a member name that implicitly calls a function to return its value, so you can hide the function and treat the name like a plain old name.

    class Trk(object):
        """Track some values and compute a function if any change"""
        def __init__(self, name, fn, **objects_to_track):
            def _trk_fn(self):
                if any(self.__dict__[x] != self.original_objects[x] for x in self.original_objects):
                    self.value = self.saved_fn(self.__dict___)
                    # now that self.value is updated, also update self.original_objects
                    for x in self.original_objects:
                        self.original_objects[x] = self.__dict__[x]
                return self.value
    
            self.original_objects = objects_to_track  # make reference copy
            self.__dict__.update(objects_to_track)
            self.name = name
            self.saved_fn = fn
            self.fn = self._trk_fn()
            self.value = self.fn()
    

    I’m sorry but I am very tired right now, and I canot finish this example. I didn’t test it either. But this shows one way to track values, and if they are different, do something different. You use it like this:

    # want to track x, y, z
    trk = Trk(x, y, z)
    trk.fn() # returns up-to-date value
    
    trk.x = new_value
    trk.fn() #detects that trk.x changed and computes new trk.value
    

    If the above works, you can use the property descriptor stuff to bind a name such that an attempt to read a value from the name will call self.fn()

    EDIT: Oh, it’s important that when self.value is updated, self.original_objects should be updated. I’ve added code to do that.

    And now I’m going to sleep!

    • 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 multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
Let's say that I have a model that handles recipes, and I want to
Let's say that I have a set of relations that looks like this: relations
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say there is a set of number 1, 2, 3, 4, 5, 6,

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.