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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:02:17+00:00 2026-05-13T13:02:17+00:00

I’m building a module where there are a whole lot of diverse objects which

  • 0

I’m building a module where there are a whole lot of diverse objects which need to be controlled by several variables.

If this were C, I would pass the objects a pointer to the variable / object I want them to watch, have the application change the variable as it needs to, and the objects in the module would “see” the new value.

In module foo.py:

class foo(object):
  def __init__(self, varToWatch):
    self.varToWatch = varToWatch
  def DoSomething(self):
    print self.varToWatch

In application, bar.py:

import foo
x = "before"
y = foo.foo(x)
y.DoSomething()
x = "after"
y.DoSomething()

This doesn’t work, of course, because the original value of x (or a reference to the original value of x?) is stored in y. You get “before” “before”.

So, I try…

  1. Passing the name of a global, and referring to the global by name in foo. No go, because the global is in the application’s space, not in the module.
  2. Using a global in the foo namespace, but I don’t know how to address that space by variable name, and we’re currently doing import * from foo rather than import foo. Changing this would be painful.
  3. Make a dictionary in the module and pass the name of a key in the dictionary to the objects in the module. Manipulate the contents of the dictionary from the application. Basically, I’m defining a new global space and sticking variables in it. I see no reason it wouldn’t work, but it feels untidy to me. Is that just me being unfamiliar with Python?

I’m not finding the kind of beautiful solutions I’m used to seeing come easily with Python, so I suspect I’m missing something.

Suggestions?

TIA,
– Tim.

  • 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-13T13:02:17+00:00Added an answer on May 13, 2026 at 1:02 pm

    How about putting the control variables in a separate module — say, settings — and doing a separate import settings? You could then have your objects watch settings.bar and settings.quux while still doing from foo import * to clutter up your main namespace. 😉 You could also use a global object to store your settings, with mostly the same effect.

    Of course, with problems such as these, one must wonder whether restructuring things to do a import foo wouldn’t help things in the long run… Impossible to judge without knowing the problem, though, so I’ll leave this problem to you.

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

Sidebar

Related Questions

I know there's a lot of other questions out there that deal with this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

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.