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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:16:06+00:00 2026-06-06T06:16:06+00:00

I have only started learning Python recently. Let me explain what I am trying

  • 0

I have only started learning Python recently. Let me explain what I am trying to accomplish. I have this .py script that basically has several functions (hard-coded into the script) that all need to be added to a single list, so that I can get the function I require by simply using the index operator as follows:

needed_function = function_list[needed_function_index]

My first attempt at implementing this resulted in the following code structure:

(imports)
function_list = []
(other global variables)

def function_0 = (...)
function_list.append(function_0)

def function_1 = (...)
function_list.append(function_1)

def function_2 = (...)
function_list.append(function_2)

(rest of code)

But I don’t like that solution since it isn’t very elegant. My goal is to be able to simply add the function definition to the script (without the append call) and the script will automatically add it to the list of functions.

I’ve thought of defining all the functions within another function, but I don’t think I’d get anywhere with those. I thought of maybe “tagging” each function with a decorator but I realized that decorators (if I understand them correctly) are called every time a function is called, and not just once.

After some time I came up with this solution:

(imports)
(global variables)

def function_0 = (...)

def function_1 = (...)

def function_2 = (...)

function_list= [globals()[x] for x in globals() if re.match('^function_[0-9]+$', x)]

(rest of code)

I like it a bit more as a solution, but my only qualm with it is that I would prefer, for cleanliness purposes, to completely define function_list at the top of the script. However, I cannot do that since an invocation of globals() at the top of the script would not contain the functions since they have not been defined yet.

Perhaps I should simply settle for a less elegant solution, or maybe I am not writing my script in an idiomatic way. Whatever the case, any input and suggestions are appreciated.

  • 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-06T06:16:08+00:00Added an answer on June 6, 2026 at 6:16 am

    You are mistaken about decorators. They are invoked once when the function is defined, and the function they return is then the value assigned to the function name, and it is that function that is invoked each time. You can do what you want in a decorator without incurring runtime overhead.

    my_functions = []
    
    def put_in_list(fn):
        my_functions.append(fn)
        return fn
    
    @put_in_list
    def function1():
        pass
    
    @put_in_list
    def function2():
        pass
    

    PS: You probably don’t need to worry about runtime overhead anyway.

    PPS: You are also trying to optimize odd things, you might be better off simply maintaining a list in your file. How often are you adding functions, and with how little thought? A list is not difficult to update in the source file.

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

Sidebar

Related Questions

I have only started learning python recently. I would still be considered a beginner.
Just started learning PySide and is having problem with QTimer I have this #!/usr/bin/python
Java script has many falsy values as I started learning. I have a program
I've recently started learning/using django; I'm trying to figure out a way to have
I recently started learning perl and have a question that I'm not finding a
I have only recently started using CakePHP and have been unable to get validation
i have only ftp credentials in which i am using this script $r=mysqldump $dbuser
I have recently started learning C++ and coming from a Ruby environment I have
I had recently started learning Python, and with all the research I decided it
I've just recently started learning HTML/CSS and I've been trying to teach myself sound

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.