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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:52:09+00:00 2026-06-12T22:52:09+00:00

Lets say I have a function bar inside a module called foo.py . Somewhere

  • 0

Lets say I have a function bar inside a module called foo.py . Somewhere inside foo.py, I want to be able to call bar() from the string “bar”. How do I do that?

# filename: foo.py
import sys

def bar():
  print 'Hello, called bar()!'

if __name__ == '__main__':
  funcname = 'bar'
  # Here I should be able to call bar() from funcname

I know that there exists some built-in function in python called ‘getattr’. However, it requires ‘module object’ to be the first parameter. How to obtain the ‘module object’ of the current module?

  • 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-12T22:52:11+00:00Added an answer on June 12, 2026 at 10:52 pm

    globals is probably easier to understand. It returns the current module’s __dict__, so you could do:

    func_I_want = globals()['bar']  #Get the function
    func_I_want()    #call it
    

    If you really want the module object, you can get it from sys.modules (but you usually don’t need it):

    import sys.modules
    this_mod = sys.modules[__name__]
    func = getattr(this_mod,'bar')
    func()
    

    Note that in general, you should ask yourself why you want to do this. This will allow any function to be called via a string — which is probably user input… This can have potentially bad side effects if you accidentally give users access to the wrong functions.

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

Sidebar

Related Questions

Let's say that I have an Erlang function, with spec. -spec foo(integer(), string()) ->
Lets say I have a javascript function call moveItem which takes two parameters source
Let's say we have def Foo(Bar=0,Song=0): print(Bar) print(Song) And I want to assign any
Lets say I have two functions: def foo(): return 'foo' def bar(): yield 'bar'
Lets say I have these classes: class Foo { public $_data; public function addObject($obj)
I want to create variables inside a function from a dictionary. Let's say I
Let's say I have two classes Foo and Bar, and I want to make
Lets say I have the function void do_work (foo?); The ? represents a 0
Lets say I have a record: -record(foo, {bar}). What I would like to do
Let's say I have this code: (function(global) { function Bar(foo) { this.foo = foo;

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.