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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:50:14+00:00 2026-05-26T07:50:14+00:00

Look at two ways of structuring my functions: class myClass: def _myFunc(self): pass def

  • 0

Look at two ways of structuring my functions:

class myClass:
    def _myFunc(self):
        pass

    def myFunc2(self):
        self._myFunc()

class myClass:
    def myFunc2(self):
        def myFunc():
            pass

        myFunc()

Will the second option be slower?
I only need to call myFunc from myFunc2, so ‘d like to hide it from my module documentation, I could use an underscore for that, but I thought it would be cleaner to have it inside the function. On the other hand I might need to call myFunc2 few hundred times per second, so “redefining” myFunc when calling myFunc2 each time might be slow… is that a good guess?

  • 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-26T07:50:14+00:00Added an answer on May 26, 2026 at 7:50 am

    The local function in the second variant won’t be compiled over and over again — it is compiled once together with the whole file, and its body is stored in a code object. The only thing that happens during the execution of the outer function is that the code object is wrapped in a new function object which is then bound to the local name myFunc.

    There might be a difference between the two variants if myFunc() takes default parameters. Their definition would be executed over and over again in the second variant, resulting in a possible performance hit.

    Exaggerated example:

    from time import sleep
    
    class MyClass:
        def _my_func(self, x=sleep(1)):
            pass
        def my_func2(self):
            self._my_func()
    
    class MyClass2:
        def my_func2(self):
            def my_func(x=sleep(1)):
                pass
            my_func()
    

    With the daft code above, myClass.myFunc2() will return immediately, while myClass2.myFunc2() takes a second to execute.

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

Sidebar

Related Questions

I have a class with two constructors that look like this: public MyClass(SomeOtherClass source)
Let's say my HTML may look one of two ways: Option 1: <h2>My header
Suppose I have two functions which look like this: public static void myFunction1(int a,
In Java, we have two ways to implement actionPerformed: One actionPerformed method per class:
Please look at the following two examples: Example 1: $variable = 'some value'; class
These two may look like they have no correlation but bear with me! In
My two tables look like this: TABLE1 TABLE2 +--------------------+ +--------------------+ |field1|field2|field3| and |field2|field4|field5| +--------------------+
Please look at the two examples below: irb(#<ActionView::Base:0x2863d58>):030:0> paintings_path => /some-nice-alias-path irb(#<ActionView::Base:0x2863d58>):029:0> self.controller_name.to_s +
I need two divs to look a bit like this: | | ---| LOGO
Let's say I have two tables that look like this: TH TH TH TH

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.