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

  • Home
  • SEARCH
  • 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 6774127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:44:58+00:00 2026-05-26T15:44:58+00:00

I am writing a module that provides one function and needs an initialization step,

  • 0

I am writing a module that provides one function and needs an initialization step, however due to certain restrictions I need to initialize on first call, so I am looking for the proper idiom in python that would allow me to get rid of the conditional.

#with conditional
module.py
initialized = False
def function(*args):
   if not initialized: initialize()
   do_the_thing(*args)

I’d like to get rid of that conditional with something like this(it does not work):

#with no conditional
module.py
def function(*args):
   initialize()
   do_the_thing(*args)
   function = do_the_thing

I realize that I cannot just use names in the module and change them at runtime because modules using from module import function will never be affected with a function=other_fun inside the module.
So, is there any pythonic idiom that could do this the right way?

  • 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-26T15:44:59+00:00Added an answer on May 26, 2026 at 3:44 pm

    The nothing-fancy way (of the methods I post here, this is probably the best way to do it):

    module.py:

    def initialize():
        print('initialize')
    def do_the_thing(args):
        print('doing things',args)
    def function(args):
        _function(args)
    def firsttime(args):
        global _function
        initialize()
        do_the_thing(args)
        _function=do_the_thing
    _function=firsttime
    

    The idea is simple: you just add a layer of indirection. function always calls _function, but _function points first at firsttime, then forever after at do_the_thing.

    test.py:

    from module import function
    function(1)
    function([2,3])
    

    Running test.py yields

    initialize
    ('doing things', 1)
    ('doing things', [2, 3])
    

    My first thought was to use a generator, but, as Triptych points out, there is no way to pass args to the function if you use a generator. So…

    here is a way using a coroutine (which, unlike a generator, allows you to send args to — as well as receive values from — the coroutine):

    module.py:

    def coroutine(func):
        # http://www.dabeaz.com/coroutines/index.html
        def start(*args,**kwargs):
            cr = func(*args,**kwargs)
            cr.next()
            return cr
        return start
    
    def initialize():
        print('initialize')
    
    def do_the_thing(*args, **kwargs):
        print('doing things', args, kwargs)
        return ('result', args)
    
    @coroutine
    def _function():
        args, kwargs = (yield)
        initialize()
        while True:
            args, kwargs = (yield do_the_thing(*args, **kwargs))
    _function = _function().send
    def function(*args, **kwargs):
        # This is purely to overcome the limitation that send can only accept 1 argument
        return _function((args,kwargs))
    

    Running

    print(function(1, x = 2))
    print(function([2, 3]))
    

    yields

    initialize
    ('doing things', (1,), {'x': 2})
    ('result', (1,))
    ('doing things', ([2, 3],), {})
    ('result', ([2, 3],))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Linux kernel module that needs to open and read files. What's
I'm writing a linux kernel module that provides a (virtual) block device (so no
I'm writing a Chrome extension, and want to write one JS file, that provides
Quick background: writing a module. One of my objects has methods that may or
I'm writing a module that has several methods. Let's consider this : package MyPackage;
I'm using dlopen() in an Apache module that I am writing so that I
I'm writing a python module for a device that interacts with a user supplied
I would need to write an Authentication Module for IIS7 that behaves exactly like
I'm writing a Drupal module that deals with creating new nodes from CSV files.
I have an Python ExcelDocument class that provides basic convenience methods for reading/writing/formatting Excel

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.