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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:41:30+00:00 2026-05-27T21:41:30+00:00

Considering the following as two versions of a very simple function in Python ,

  • 0

Considering the following as two versions of a very simple function in Python, the question is which one is preferred over another and why?

Is there any extra memory usage and or CPU usage for case 1 compared to case 2 in which additional assignment has been used?

case 1:

def f(x):
    y = x*x
    return y

case 2:

def f(x):
    return x*x

From my point of view, the case 1 is clearer than the other one especially in case of complex computations and providing many returning objects. On the other hand, the case 2 looks very compact and would be considered as a very attractive so. Since the simple is simply efficient. What do you think?

update:
I didn’t know there is disassembler in Python! It was so amazing when I learned the answer of my question and the way to check similar ideas. Special appreciation.
from dis import dis as dasm
dasm(f)

  • 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-27T21:41:31+00:00Added an answer on May 27, 2026 at 9:41 pm

    If you want to know what Python style is preferred, look at the code in the standard library:

    $ grep return Lib/*py
    

    You will see that the case 2 style is the most common.

    I personally use the assignment form only when the variable name is needed to add clarity to the code:

    normalized_query = query.replace(' ', '').lower()
    return normalized_query
    

    This code introduces a very small bit of overhead for the assignment:

    >>> def f(x):
            return x + 2
    
    >>> def g(x):
            y = x + 2
            return y
    
    >>> dis(f)
      2           0 LOAD_FAST                0 (x)
                  3 LOAD_CONST               1 (2)
                  6 BINARY_ADD          
                  7 RETURN_VALUE        
    >>> dis(g)
      2           0 LOAD_FAST                0 (x)
                  3 LOAD_CONST               1 (2)
                  6 BINARY_ADD          
                  7 STORE_FAST               1 (y)
    
      3          10 LOAD_FAST                1 (y)
                 13 RETURN_VALUE 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm considering the following: I have some data stream which I'd like to protect
Considering the criteria listed below, which of Python, Groovy or Ruby would you use?
Considering the following two usage scenarios (exactly as you see them, that is, the
take two following classes and their constructors as samples: class One{ public: One(int a,int
My DB have two tables - Question and Topic. To implement many-to-many relation, there
Considering following array and two series of assignments: char charArray[3]; charArray[0]='a'; charArray[1]='b'; charArray[2]='c'; char
I posted the following question on another thread: Does anybody know of a good
Given the following code without considering friendship between two classes: class OutSideClass { ...
Considering following code public class A { public static void main(String[] args) { new
Considering the following architecture: a base object 'Entity' a derived object 'Entry:Base' and a

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.