1

I am a beginner of python and have a question, very confusing for me. If I define a function first but within the function I have to use a variable which is defined in another function below, can I do it like this? Or how can I import the return things of another function into a function? for example:

def hello(x,y):
    good=hi(iy,ix)
    "then do somethings,and use the parameter'good'."
    return something

def hi(iy,ix):
    "code"
    return good
S.Lott
  • 384,516
  • 81
  • 508
  • 779
NONEenglisher
  • 1,715
  • 4
  • 15
  • 17
  • Your question is not entirely clear to me. How are the two methods related? Do they belong to the same class? – Joachim Sauer Dec 10 '08 at 22:23
  • yes,they a in same class,just the fist function need the result from the second function to do something. – NONEenglisher Dec 10 '08 at 22:28
  • I really don't understand the issue with this, you just assign the return values of a function call to a variable inside the method, as in any other case. – Lucas S. Dec 10 '08 at 22:35
  • I think the questioner is misunderstanding the problem they have. If you could show the actual code and what you want to do, we will be able to help you better. – Harley Holcombe Dec 10 '08 at 22:36

5 Answers5

5

The scope of functions hello and hi are entirely different. They do not have any variables in common.

Note that the result of calling hi(x,y) is some object. You save that object with the name good in the function hello.

The variable named good in hello is a different variable, unrelated to the variable named good in the function hi.

They're spelled the same, but the exist in different namespaces. To prove this, change the spelling the good variable in one of the two functions, you'll see that things still work.


Edit. Follow-up: "so what should i do if i want use the result of hi function in hello function?"

Nothing unusual. Look at hello closely.

def hello(x,y):
    fordf150 = hi(y,x)
    "then do somethings,and use the variable 'fordf150'."
    return something

def hi( ix, iy ):
    "compute some value, good."
    return good

Some script evaluates hello( 2, 3).

  1. Python creates a new namespace for the evaluation of hello.

  2. In hello, x is bound to the object 2. Binding is done position order.

  3. In hello, y is bound to the object 3.

  4. In hello, Python evaluates the first statement, fordf150 = hi( y, x ), y is 3, x is 2.

    a. Python creates a new namespace for the evaluation of hi.

    b. In hi, ix is bound to the object 3. Binding is done position order.

    c. In hi, iy is bound to the object 2.

    d. In hi, something happens and good is bound to some object, say 3.1415926.

    e. In hi, a return is executed; identifying an object as the value for hi. In this case, the object is named by good and is the object 3.1415926.

    f. The hi namespace is discarded. good, ix and iy vanish. The object (3.1415926), however, remains as the value of evaluating hi.

  5. In hello, Python finishes the first statement, fordf150 = hi( y, x ), y is 3, x is 2. The value of hi is 3.1415926.

    a. fordf150 is bound to the object created by evaluating hi, 3.1415926.

  6. In hello, Python moves on to other statements.

  7. At some point something is bound to an object, say, 2.718281828459045.

  8. In hello, a return is executed; identifying an object as the value for hello. In this case, the object is named by something and is the object 2.718281828459045.

  9. The namespace is discarded. fordf150 and something vanish, as do x and y. The object (2.718281828459045), however, remains as the value of evaluating hello.

Whatever program or script called hello gets the answer.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • is that means the two functions are totally have relations?so what should i do if i want use the result of hi function in hello function? thanks alot – NONEenglisher Dec 10 '08 at 22:56
  • There's a term for the situation where 'hello' calls 'hi' and 'hi' calls 'hello'. We call it recursion. A simpler case is when a function can call itself. It's not usually considered a beginner's topic, but if you run a search on recursion you'll find plenty of information. Welcome to programming. – Jeffrey Martinez Dec 10 '08 at 23:56
3

If you want to define a variable to the global namespace from inside a function, and thereby make it accessible by other functions in this space, you can use the global keyword. Here's some examples

varA = 5   #A normal declaration of an integer in the main "global" namespace

def funcA():
    print varA #This works, because the variable was defined in the global namespace
               #and functions have read access to this.
def changeA():
    varA = 2  #This however, defines a variable in the function's own namespace
              #Because of this, it's not accessible by other functions.
              #It has also replaced the global variable, though only inside this function
def newVar():
    global varB #By using the global keyword, you assign this variable to the global namespace
    varB = 5

def funcB():
    print varB #Making it accessible to other functions

Conclusion: variables defined in a function stays in the function's namespace. It still has access to the global namespace for reading only, unless the variable has been called with the global keyword.

The term global isn't entirely global as it may seem at first. It's practically only a link to the lowest namespace in the file you're working in. Global keywords cannot be accessed in another module.

As a mild warning, this may be considered to be less "good practice" by some.

Zoomulator
  • 20,774
  • 7
  • 28
  • 32
2

your example program works, because the two instances of 'good' are different variables (you just happen to have both variables with the same name). The following code is exactly the same:

def hello(x,y):
    good=hi(iy,ix)
    "then do somethings,and use the parameter'good'."
    return something

def hi(iy,ix):
    "code"
    return great
Jimmy
  • 89,068
  • 17
  • 119
  • 137
2

More details on the python scoping rules are here :

Short Description of Python Scoping Rules

Community
  • 1
  • 1
Rizwan Kassim
  • 7,931
  • 3
  • 24
  • 34
1

The "hello" function doesn't mind you calling the "hi" function which is hasn't been defined yet, provided you don't try to actually use the "hello" function until after the both functions have been defined.

nakedfanatic
  • 3,108
  • 2
  • 28
  • 33