Iterator recursive format

Function and use first-class objects

  1. As it may be assigned to a variable value

    def func():
        print(1)
    a = func
    print(a)
    print(func)
    
    输出的结果:   你犯浑的原因在于,不敢判断 func函数是不用运行的
    <function func at 0x105d58e18>
    <function func at 0x105d58e18>
  2. As elements stored in the container

def func():
    print(1)
lst = [func,func,func]
print(lst)
for i in lst:
     i()       #这就是相当于 func()   函数调用
#

[<function func at 0x10f49be18>, <function func at 0x10f49be18>, <function func at 0x10f49be18>]
1
1
1

3. function name as a parameter of the function

def func(f):
    print(f)    #调用的 foo 的内存地址
def foo():      #没有调用  不用执行
    print(233)
func(foo)

#结果
<function foo at 0x1016642f0>

4. The function name can be used as the return value of the function

def func():
    def foo():
        print(233)
    return foo      #返回给 func foo的内存地址
print(func())              

#<function func.<locals>.foo at 0x10e0c92f0>

Plus a determination

def f1():
    def f2():
        print(1)
        def f3():
            print(123)
        return f2       #这里返回的是 f2 的内存地址,
    ret = f2()
    return ret          #所以这里的内存地址
print(f1())

f- format

% S and f can format

s = f"你好{'常鑫'}"  #填充字符串
s1 =F"你好{'常鑫'}"
print(s,s1)
s ="123"
s = f"你好{s}"    #可以存放变量
print(s)
s = f"你好{17+12}"   #填充计算公式
print(s)
a = 10
b = 20
s = f"你好{a if a > b else b}"     #填充三元运算符
print(s)
#你好  20
s1 = f"{{{{'常鑫'}}}}"  #两个大括号是一个
print(s1)
s1 = f"{print(1233)}"    #后面可以是函数   这个有疑惑,需要解决一下
print(s1)

# 1233
# None
def foo():
    def func():
        a = 1
        return a
    return func()
s1 = f"{foo()}"     #现在看这个程序已经没有啥问题
print(s1)

#  1     
lst = [1,2,3,4,5,6,7,8]
s = f"{lst[:5]}"
print(s)
#  [1, 2, 3, 4, 5]
dic = {"key":1,"key2":2}
s1 = f"{dic['key']}"
print(s1)
#  1

Iterator

When a large amount of data containers when using the iterator, the file handle

Iterator advantages: saving memory, inert mechanism

Iterator drawback: the use of inflexible, complicated operation, can not directly see all values

Iterator features:

  • One-time
  • Not retrograde
  • Inert mechanism (one by one to, do not give) ----- save memory

: Tools

Iterables ---- list, dict, str, set, tuple

It has many proprietary methods, with __iter__ method is an iterative objects, flexible

Iterables not necessarily iterator, but iteration object must be iterable.

To see whether the method is iterative object

list.__iter__()
str.__iter__()
dic.__iter__()
tuple.__iter__()
set.__iter__()

Method Two

See source

Method three: dir

print(dir(list))

Method Four

Official statement has only __iter__ is to iterables

Can advantage iteration object:

1. Use flexible

2. Direct View value

Iteration can drawback object:

Memory consumption

Value ways:

list, tuple, str - index

dict-- key

set-- direct value

for

Iterator: iter () will take the conversion iterables iterator

** official statement as long as __iter__ method __next__ iterators **

Values ​​were based on a location to stay

s = "152132"
news_s = s.__iter__()   #将可迭代对象转化为迭代器
print(news_s)
print(news_s.__next__())
print(news_s.__next__())
print(news_s.__next__())
print(news_s.__next__())
print(news_s.__next__())

#
1
5
2
1
3

for circulation principle

s = "12332"
new_s = s.__iter__()
while True:
    try:
        print(new_s.__next__())
    except StopIteration:  # 捕获错误
        break

s = "12332"
new_s = s.__iter__()
while True:
    try:
        print(new_s.__next__())
    except Exception:  # 万能异常捕获
        break

Recursion

Their learning --- learn Baidu

Recursive conditions: 1. calls itself (the official word, constantly calls itself)

2. have a clear termination condition

Meet the above two is valid recursive

Delivery: band performed until the end condition

Return: From the end of the retreat back condition

Official statement maximum level of 1000, the actual test can be modified 998/997

def age(n):
    if n == 4:
        return 18
    else :
        return age(n+1)-2
print(age(1))

Guess you like

Origin www.cnblogs.com/hualibokeyuan/p/11209957.html