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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:33:23+00:00 2026-06-14T06:33:23+00:00

I know about inlining, and from what I checked it is not done by

  • 0

I know about inlining, and from what I checked it is not done by the Python’s compiler.

My question is : is there any optimizations with the python’s compiler which transforms :

print myList.__len__()
for i in range(0, myList.__len__()):
    print i + myList.__len__()

to

l = myList.__len__()
print l
for i in range(0, l):
    print i + l

So is it done by the compiler ?
If it is not : is it worth it to do it by myself ?

Bonus question (not so related) : I like to have a lot of functions (better for readability IMHO)… like there is no inlining in Python is this something to avoid (lots of functions) ?

  • 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-14T06:33:24+00:00Added an answer on June 14, 2026 at 6:33 am

    No, there isn’t. You can check what Python does by compiling the code to byte-code using the dis module:

    >>> def test():
    ...     print myList.__len__()
    ...     for i in range(0, myList.__len__()):
    ...         print i + myList.__len__()
    ... 
    >>> import dis
    >>> dis.dis(test)
      2           0 LOAD_GLOBAL              0 (myList)
                  3 LOAD_ATTR                1 (__len__)
                  6 CALL_FUNCTION            0
                  9 PRINT_ITEM          
                 10 PRINT_NEWLINE       
    
      3          11 SETUP_LOOP              44 (to 58)
                 14 LOAD_GLOBAL              2 (range)
                 17 LOAD_CONST               1 (0)
                 20 LOAD_GLOBAL              0 (myList)
                 23 LOAD_ATTR                1 (__len__)
                 26 CALL_FUNCTION            0
                 29 CALL_FUNCTION            2
                 32 GET_ITER            
            >>   33 FOR_ITER                21 (to 57)
                 36 STORE_FAST               0 (i)
    
      4          39 LOAD_FAST                0 (i)
                 42 LOAD_GLOBAL              0 (myList)
                 45 LOAD_ATTR                1 (__len__)
                 48 CALL_FUNCTION            0
                 51 BINARY_ADD          
                 52 PRINT_ITEM          
                 53 PRINT_NEWLINE       
                 54 JUMP_ABSOLUTE           33
            >>   57 POP_BLOCK           
            >>   58 LOAD_CONST               0 (None)
                 61 RETURN_VALUE        
    

    As you can see, the __len__ attribute is looked up and called each time.

    Python cannot know what a given method will return between calls, the __len__ method is no exception. If python were to try to optimize that by assuming the value returned would be the same between calls, you’d run into countless different problems, and we haven’t even tried to use multi-threading yet.

    Note that you would be much better off using len(myList), and not call the __len__() hook directly:

    print len(myList)
    for i in xrange(len(myList):
        print i + len(myList)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There's two things about inlining: The inline keyword will be ignored if the compiler
I know about Papervision 3D. However, alot of the realism there comes from textures.
I know about this question: Which (third-party) debug visualizers for Visual Studio 2005/2008 do
I know about the not-null attribute. Is there one for enforcing the minimum length
I know about Adobe's Flash Detection kit - but is there any way to
I know about the LINEABOVE and LINEBELOW styles, i was wondering if there is
I know about Options->Fonts and Colors. But there is no option for F# strings(i
I know about all the issues with session fixation and hijacking. My question is
I know about Flash's localToGlobal and globalToLocal methods to transform coordinates from the local
I know about flag -XX: PretenureSizeThreshold which can be used to set to limit

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.