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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:46:43+00:00 2026-05-15T06:46:43+00:00

Suppose I have a function like: def foo(): x = ‘hello world’ How do

  • 0

Suppose I have a function like:

def foo():
    x = 'hello world'

How do I get the function to return x, in such a way that I can use it as the input for another function or use the variable within the body of a program? I tried using return and then using the x variable in another function, but I get a NameError that way.


For the specific case of communicating information between methods in the same class, it is often best to store the information in self. See Passing variables between methods in Python? for details.

  • 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-15T06:46:44+00:00Added an answer on May 15, 2026 at 6:46 am

    You can use the return statement to return a value from a function, this does not make it so that the returned variable (value) becomes available in the scope of the caller. To use the value in the caller you can either use it directly in a statement or capture the value in a variable.

    Here’s some code to demonstrate this:

    def foo():
        x = 'hello world'
        return x  # return 'hello world' would do, too
    
    foo()
    print(x)   # NameError - x is not defined outside the function
    
    y = foo()
    print(y)   # this works
    
    x = foo()
    print(x)   # this also works, and it's a completely different x than that inside
               # foo()
    
    z = bar(x) # of course, now you can use x as you want
    
    z = bar(foo()) # but you don't have to
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 515k
  • Answers 515k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What you render with papervision3d ends up in a View(basic,bitmap,… May 16, 2026 at 6:25 pm
  • Editorial Team
    Editorial Team added an answer I've found my own answer: in my case, the error… May 16, 2026 at 6:25 pm
  • Editorial Team
    Editorial Team added an answer As far as I remember, TreeGrid didn't work particularly well… May 16, 2026 at 6:25 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Suppose I have a function like: def myfun(a, b, c): return (a * 2,
Suppose I have a function like this: def getNeighbors(vertex) which returns a list of
Suppose I have a function that performs some task (this is in Python pseudocode):
Suppose I have a family of types which support a given member function, like
Suppose I have a namedtuple like this: EdgeBase = namedtuple(EdgeBase, left, right) I want
Suppose I have a state monad such as: data Registers = Reg {...} data
Suppose I have a .NET dll with class 'B' and function 'C'. I then
Suppose that I have an array defined by: data = np.array([('a1v1', 'a2v1', 'a3v1', 'a4v1',
I have foreach function which calls specified function on every element which it contains.
Suppose i have this sql statement and I have executed a sql command to

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.