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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:32:00+00:00 2026-06-12T15:32:00+00:00

I have a python module_A which is doing something like this: myvar = Hello

  • 0

I have a python module_A which is doing something like this:

myvar = "Hello"

def message():
    print myvar

From another module, I want to import the function message:

from module_A import message

But this is defining myvar, which I do not want (for reasons not really relevant here). I guess what I need is to make a distinction between importing things from a module, and using any of the imported symbols. I actually want myvar to be initialized only when I use message.

I there any kind of python hook that I could use to initialize stuff whenever a function or class is accessed? I would like something similar to the following:

module_setup_done = False
def setup():
    global module_setup_done, myvar
    if not module_setup_done:
        myvar = "Hello"
        module_setup_done = True

def message():
    setup()
    print myvar

But, to reduce clutter, I would like this to be called automatically, something like this:

def _init_module():
    global myvar
    myvar = "Hello"

def message():
    print myvar

Where _init_module() would be called only once, and only the first time that something in the module is accessed, not when something is imported.

Is there support for this kind of pattern in python?

  • 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-12T15:32:01+00:00Added an answer on June 12, 2026 at 3:32 pm

    No, there is no built-in support for this. If you want to initialize things separately from importing the module, write a function that does that initialization, then call the function when you want to initialize those things. If you want to “automatically” initialize things when other things are called, you have to handle that yourself with code along the lines of what you already posted.

    If you find yourself doing that, though, you’re probably doing something unpythonic. You don’t give details about why you’re doing it here, but in general this sort of implicit initialization is not a good practice in Python. Things are initialized when you explicitly tell them to be, for instance by importing a module or calling some sort of initialization function. Why do you feel the need to have this separate, implicit initialization step?

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

Sidebar

Related Questions

In a Python system for which I develop, we usually have this module structure.
I am really stumped on this one. I have a simple python wrapper which
I have a python file testHTTPAuth.py which uses module deliciousapi and is kept in
I've got a Python program which is reading data from a serial port via
So I have a text file which has the script of Act 1 from
I'm about to start a program using Python which is mostly doing polling, it
In python, if you need a module from a different package you have to
I have a problem with dbus and python. Running python from the command line,
I have a python module which generates large data files which I want to
I have a module-level variable in my Python 2.6 program named _log, which PyLint

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.