Functions of Python (ii)

Source: Internet
Author: User

1, the variable of the function

2, the return value of the function

1, the variable of the function

The variables of the function are divided into local variables and global variables.

This x is a local variable, and after the function is executed, the x variable is destroyed and only valid within the function.

x = 100def Fun ():p rint x Print x  

The x = 100 ' is a global variable and can also be called inside a function. However, the call can only be used for print, and other operations will be error. Like what:

x = 100def Fun ():p rint x x+=1

An action like this that executes the x+=1 will cause an error.

Of course, you can declare a global variable inside a function by using the global keyword, so that you can do other things after you declare it.

x = 100def Fun (): Global xx +=1print x

1.1, variables defined inside the function can also be used outside the function, and the keyword Global keyword is also used.

def fun ():    global y    y = 1fun () Print Y

So y can also output, but only if you have to call the function first.

1.2, function locals, which variables are in the output script execution. If you write to the inside of a function, all variables inside the function are returned. If it is written inside the program, then output all the variables of the program.

def fun (): x = 1y = 2print locals ()

View all variables of the program, output as a dictionary.

#!/usr/bin/env pythonprint ' Hello,world ' pritn locals ()

2, function return value (return)

function if there is no return statement by default, the function will return none by default. The function encounters a return end run.

def test ():    print ' Test ' Print test ()

The code above returns: Test and none. Why would there be none? Because the function does not have a Retun statement, it will return none by default.

def Test2 (Var):    if Str (VAR). IsDigit ():        return ' This is number  '    return ' Thisi was not number ' test2 (1)

It can be found that the if is behind the else. It says that when the code executes to return, the function stops running. So as long as the above if is executed, the back return will not be executed.

Summary:

1, variables are divided into global variables and local variables. If you define a global variable, if you call this local variable inside the function, you can only use Print's keyword output (in other words, the function internally knows the global variable, but cannot manipulate the global variable), so if you are inside the function
Using the Global keyword is telling the program that I want to manipulate the global variables. At this point, the function can formally manipulate global variables.

2, locally defined variables if you want to become global variables, you can use the Global keyword. This local variable can be called a global variable, but only if you want to execute the function.

Functions of Python (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.