Python variable scope, function and parameter

Source: Internet
Author: User

One, the value of the tuple:

In general, the function pass parameter is 1 to 1, here X, Y is 2 parameters, according to the reason to pass 2 parameters, if the direct transfer of the ancestor, is actually passed a parameter

def Show (x, y): ...      Print  >>> a = (ten )>>> Show (A, 20) 100 

And if you want to pass a meta-ancestor (2 items) to X and Y, pass the time to use *a, if a function to 3 parameters, you can not pass 2 Ganso

def Show (x, y): ...      Print " %s:%s " %>>> a= (10,20)>>> Show (*a)10:20>>> b= (10,20,30 )>>> Show (*b) Traceback (most recent):  "<stdin>  " in <module>2 arguments (3 given)
Print "%s:%s"% (x, y)

This percent%s is similar to the C-language printf, where the placeholder represents a string to interpret, and the trailing% (x, y) is the value of the pass. X passed to the first%s, Y to the second%s

If the value is not passed later, the string itself is printed

Print " %s:%s " ' Hello ' ' GHOSTWU ' ) HELLO:GHOSTWUprint"%s:%s"%s:%s

Second, the variable scope is the same as JavaScript

The outside of the function defines a global variable that can be accessed inside or outside the function

The function defines a local variable, which is released after the function call is complete.

>>> myname ='GHOSTWU'>>>defShow (): ...Printmyname ... x= 10... >>>Show () Ghostwu>>>Xtraceback (most recent): File"<stdin>", Line 1,inch<module>Nameerror:name'x'  is  notdefined>>>myname'GHOSTWU'>>>

The Global keyword can change a local variable to a global variable, which is not the same as PHP (PHP refers to a global variable that is used inside a function)

def Show ():      ... Global a ...      .. = Ten>>> Show ()>>> a10

Global variables and local variables with the same name, local variables are accessed inside the function, global variables are outside

def Show ():      ... = Ten      ... Print  >>> a100>>> show ()10>>> a100

If the local variable with the same name is global and the function is executed, then the function executes after the output of the variable, which is the value in the local variable.

def Show ():      ... Global a ...      .. = 2>>> a1>>> show ()>>> a2

Python variable scope, function and parameter

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.