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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:30:05+00:00 2026-05-14T23:30:05+00:00

I want to be able to call a global function from an imported class,

  • 0

I want to be able to call a global function from an imported class, for example

In file PetStore.py

class AnimalSound(object):
   def __init__(self):
      if 'makenoise' in globals():
         self.makenoise = globals()['makenoise']
      else:
         self.makenoise = lambda: 'meow'

   def __str__(self):
      return self.makenoise()

Then when I test in the Python Interpreter

>>> def makenoise():
...    return 'bark'
...
>>> from PetStore import AnimalSound
>>> sound = AnimalSound()
>>> sound.makenoise()
'meow'

I get a ‘meow’ instead of ‘bark’. I have tried using the solutions provided in python-how-to-make-a-cross-module-variable with no luck.

  • 1 1 Answer
  • 2 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-14T23:30:06+00:00Added an answer on May 14, 2026 at 11:30 pm

    The globals() call returns the globals of the module in which the call is lexically located; there is no intrinsic “dynamic scoping” in Python — it’s lexically scoped, like just about every modern language.

    The solid, proper way to obtain the effect you desire is to explicitly pass to the initializer of AnimalSound the callable it’s supposed to use to “make noise”: i.e., the class should be

    class AnimalSound(object):
       def __init__(self, makenoise=lambda: 'meow'):
           self.makenoise = makenoise
    
       def __str__(self):
           return self.makenoise()
    

    and the call should be

    sound = AnimalSound(makenoise)
    

    There are practicable but less-sound solutions, such as the caller passing its own globals() (but that needlessly constrains the name of the callable!), or even (shudder) communicating via covert channels like the other answer advocates (that would be a potential disaster if you had two instantiations of AnimalSound built according to the same principle in two separate modules, etc, etc). But, “explicit is better than implicit”, and clean, safe, overt communication leads to clean, safe, robust system architectures: I earnestly recommend you choose this route.

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

Sidebar

Related Questions

I want to be able to call a function from the on success region
I want to be able to call a function, that will set one or
I want to be able to call 'person.Person. class ' and get back 'class
Basically I want to be able to call jQuery from FireBug, place code that
When redefining a class method I want to be able to call super, just
I want to be able to make an HTTP call updating some select boxes
I want to be able to generate PDF output from my (native) C++ Windows
How do I call a class from a string containing that class name inside
I am making Titanium mobile project where I want to make one global function
hey guys, I have mypage.com/user-login.php?action=register and I want to be able to call mypage.com/register

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.