Tip: Scope of Pyhon

Source: Internet
Author: User
Tags throw exception

http://www.cnblogs.com/wupeiqi/p/5649402.html   Five words take care of JavaScript scopes
from javascript = = Pyhon Scope Almost consistent __:one, "no block-level scope in JavaScript"There is a block-level scope in Java or C #, which is: curly braces are also a scope.     There is no block-level scope in the Python \ JavaScript language.

# Scopes
# python no block level scope.
# java/c#
may not take effect.
if1==1:
Name=' Alex '
Print(name) #effective. Alex

forIinchRange(Ten):
Name=I
Print(name) #9


# python takes a function as a scope.
deffunc():
name= ' Alex ' #name只在代码块中可以引用.
Func ()
Print(name) #cannot use output, error.

Second, the Python \javascript uses the function scope
    when executing console.log (XO), its search order is from inside to outside according to the scope chainthe priority of the search,if the inner layer does not look up gradually, until the throw exception is not found.



Iv. python \javascript has been created before the scope chain is executed

Python's scope is created before it is executed, and it is only necessary to follow the scope chain to find it later.

 

Scope chain
The scope is determined before execution. In the scope of the location, look up

# Example 1
Name=' Alex '
defF1():
Print(name)
defF2():
name=' Eric '
F1 ()

F2 ()
#Execution Result:
#alex


 Example 2 ,

#原理: But whatever function is used, such as F1 (), will be from F1 () your own function to look up. There's nothing to do with where you put the function.
Name=' Alex '
defF1(): #f1the scope of the location, looking up.
Print(name)

defF2():
name=' Eric ' #Inner Scope
returnF1

Ret=F2 ()
# print (ret)
RET ()

#Results
# <function F1 at 0x0000026c7d81df28>
# Alex

 
Sina interview questions:

# Example3 Advanced.

Li=[x+ - forXinchRange(Ten)ifX>6]
Print(LI)

#Execution Result:
# [107, 108, 109]

# Sina Interview questions:
# inLambdaas a list of elementsli=[Functions , functions, Functions, ]

Li1=[Lambda:X forXinchRange(4)]#lielements in a list, "functions, Functions, Functions",
Jit=[Lambda:1]
Li3=[x forXinchRange(Ten)]
Print(Lambda:9)

Print(li1[0])#a function name was generated and was not executed.

Ret=li1[0]()
Print(ret)

Print(li2[0]())

Print(LI3)

###########
Print([Lambda:9,][0]())#based on content--self-definedLambda ()executes the function.

#Execution Result:
# <function <listcomp>.<lambda> at 0x00000263d1ca80d0>
# 3
# 1
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# 9


























From for notes (Wiz)

Tip: Scope of Pyhon

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.