Python path-Application of function name, closure, iterator

Source: Internet
Author: User
Tags closure iterable

# # #函数名的应用
# def func ():
# Print (666)
# func ()
#
# 1. Function name is the memory address of the function
# Print (func) #结果 <function func at 0x000002ac2188c1e0>

# 2. Function names can be used as variables
# a = 2
# b = A
# c = b
# Print (c)

# def func1 ():
# Print (666)
# F1 = func1 ()
# F2 = F1
# F2 ()

# 3. Function name can be used as parameter of function
# def func1 ():
# Print (666)
# def FUNC2 (x):
# print (x) #<function func1 at 0x0000026c3d983bf8>
# x ()
# func1 ()

# def func ():
# Print (666)
# def func1 ():
# print (123) # Questions
# func ()
# def FUNC2 (x):
# x ()
# FUNC2 (FUNC1)

# 4. Function names can be used as return values of functions
# def Wraaper ():
# def inner ():
# print (' inner ')
# return Inner
# ret = Wraaper () # inner
# RET () # inner ()

# def FUNC2 ():
# print (' in Func ')
#
# def func3 (x):
# print (' in func3 ')
# return X
# F1 = func3 ((Func2 ()))

# F1 ()
# 5. Function name can be used as an element of the container class type
# a = 6
# b = 4
# c = 5
# L1 = [A,b,c]
# print (L1)

# def func1 ():
# print (' in func1 ')
# def FUNC2 ():
# print (' in Func2 ')
# def func3 ():
# print (' in Func2 ')
# def FUNC4 ():
# print (' in Func4 ')
# L1 = [Func1,func2,func3,func4]
# for I in L1:
# I ()

# to the function name above, the first class object

# #返回当前位置的局部变量的字典globals () Locals ()
# # GLOBALS () #返回全局变量的一个字典
# # Locals ()

# def func1 ():
# a= 2
# b = 3
# Print (Globals ()) #返回全局变量的一个字典
# func1 ()

# def func1 (): #返回当前位置局部变量的一个字典
# a = 2 # {' A ': 2, ' B ': 3}
# b = 3
# print (Locals ())
# func1 ()

# Closed Bag
# inner layer function reference to variables (non-global variables) of the outer layer function
# and return, so that a closure is formed

# def Wraaper ():
# name = ' Alex ' #最里层函数调用了上一级的局部变量
# def inner (): #内存种不会立刻消失, again inside the function
# print (name) #调用的话可以调用成功
# Print (inner.__closure__)
# inner ()
# def qwe ():
# print (name)
# qwe ()
# return Inner
#
# Wraaper ()
# print (name)

The effect of "closures"
When the program executes, it encounters the function execution, he will open up a space in memory,
Local namespace, if a closure is formed inside the function
Then he will not disappear with the end of the function.
‘‘‘
#可迭代对象

#对象内部含有_iter_方法就是可迭代对象.
# an Iterative object satisfies an iterative protocol
# Can iterate objects
#str List Dict tuple set Ranger ()

# S1 = ' STRs '
# Print (dir (S1)) #可以查字符串的所有方法

# to determine whether an object is an iterative object
# The first method
# dic = {' name ': ' Alex ', ' age ': ' 18 '}
# print (' __iter__ ' in Dir (DIC))
# Print (dir (DIC))

# S1 = [+]
# Print (dir (S1))
# The second method
# from Collections Import iterable
# from Collections Import Iterator
#
# Print (Isinstance (' Alex ', iterable))
# Print (Isinstance (' Alex ', Iterator)) # True

# Print (Isinstance (' Alex ', str))

# iterators
# object contains __iter__ method and contains __next_ method is iterator
# A file handle is an iterator
# f = open (' T1.txt ', encoding= ' utf-8 ', mode= ' R ')
# print (' __iter__ ' in Dir (f)) #True
# print (' __next__ ' in Dir (f)) #True
# print (' __iter__ ' in Dir (dict)) #False
# print (' __next__ ' in Dir (dict)) #False

# dic = {' name ': ' Alex '}
# print (' __iter__ ' in Dir (DIC)) #True

# The difference between an iterative object and an iterator
# An Iterative object cannot be evaluated, and an iterator can take a value
# An Iterative object can be converted to an iterator

# lis = [1,2,3,4,5,6]
# ite1 = lis.__iter__ () #迭代器
# ite1 = iter (LIS)
# print (ite1) #itel的内存地址
# Print (ite1.__next__ ()) #取迭代器第一个值

# L1 = {' name ': ' Alex '}
# L2 = l1.__iter__ ()
# # L3 = iter (L2)
# Print (l2.__next__ ())

# L1 = [1,2,3,4,5,6,7,8,9,10]
# L2 = l1.__iter__ ()
# Print (l2.__next__ ())

# convert an iterative object to an iterator
# iterator = object can be iterated. __iter__ ()

# How do iterators take values? Next time, go to a value
# Print (ite1.__next__ ())
# Print (ite1.__next__ ())
# Print (ite1.__next__ ())
# Print (ite1.__next__ ())
#调用__next__取值

# 1. An iterative object cannot be evaluated, and an iterator can take a value
# 2. Iterators Save Memory
# 3. Iterators can only take one value at a time
# 4. iterators are one-way, one way to the head

# Use exception handling to stop error
# try

# S1 = ' 1233456 '
# s = s1.__iter__ ()
# while 1:
# try:
# Print (s.__next__ ())
# except Stopiteration:
# break

Python path-Application of function name, closure, iterator

Related Article

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.