Default value a function of the python closures and iterator

# In the function, if the default parameter is a mutable data type, time of call if someone else changed his position to see the change too. 
DEF the Fn (Val, List = []):
    list.append(val)
    return list

Print (the Fn (5 ))
 # This position us to pass value val print the results must be [5]

print(fn(6))

# This position we continue to find value-passing result is [5, 6], then that is a function of the parameter default values are the same, and not every call function produce new default
# # In the function if the default parameter is a mutable data type, time of call if someone else changed his position to see the change too. 
# DEF the Fn (Val, List = []): 
#      List. the append (Val) 
#      return List
#
# Print (the Fn (5)) 
# # This position us to pass value val print the result would be [5]
#
# print(fn(6))

# This position we continue to find value-passing result is [5, 6], then that is a function of the parameter default values are the same, and not every call function produce new default



# What are the closures and closures practical effect 
# We know that when the function declaration is not executed, will open up space in memory, garbage collection mechanism within python, this will be recovered after the function completes only when call RAM

def fn1():
    val = 100
    print(val)
fn1 ()
# This time after performing fn1, it's in that memory will be cleared, and the closure of the role is to make this memory-resident

def fn2():
    val  = 100
    def fn3():
        return val
    print(fn1.__closure__)
    print(fn2.__closure__)
    print(fn3.__closure__)
    return fn3
print(fn2()())

# Can be used to detect function is not __closure__ closure 
# None 
# None 
# (<0x000001366E928B28 Cell AT: AT 0x00007FFAD5407D60 int Object>,) the result is a closure fn3


# Srt in python, list, tuple, dict, set , file these are iterables, then why they are iterables it, because they followed the iterable protocol 

S = " Truth conclusion " 
# Print (dir (S )) 
# [ '__add__', 'the __class__', '__contains__', '__delattr__', '__DIR__', 'the __doc__', '__eq__', '__format__', '__ge__', '__getattribute__', 'the __getitem__', 
#   ' __getnewargs__ ',' __gt__ ',' a __hash__ ',' the __init__ ',' __init_subclass__ ',' the __iter__ ',' __le__ ',' the __len__ ',' __lt__ ',' __mod__ ',' __mul__ ',' __ne__ ', 
#   ' __new__ is ',' __reduce__ ',' __reduce_ex__ ',' __repr__ ','__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize',
#  'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal',
#  'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition',
#  'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
#  'zfill']

# We have found __iter__ this string iterators, and have the iterator is iterable `

# Print (the dir (S .__ ITER __ ())) 
# [ 'the __class__', '__delattr__', '__DIR__', 'the __doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', 'a __hash__', 'the __init__', '__init_subclass__', 
#   'the __iter__', '__le__', '__length_hint__', '__lt__', '__ne__', '__new__ is',' __next__ ',' __reduce__ ',' __reduce_ex__ ',' __repr__ ',' __setattr__ ',' __setstate__ ', 
#   ' __sizeof__ ',' __str__ ',' __subclasshook__ '] 
# when we execute it gets iterator found iterator iter also contain iterables 

ITR = S.__iter__ ()
 # get iterators 
Print (ITR. __next__ ())
 Print (ITR. __next__ ())
 Print (ITR.__next__ ())
 Print (ITR. __next__ ())
 # true 
# # with 
# # fixed 
# # s 
#   , respectively, the printed words 

LST = [1,2,3,4,5 ]

itr2 = lst.__iter__()

print(itr2.__next__())
print(itr2.__next__())
print(itr2.__next__())
print(itr2.__next__())
print(itr2.__next__())

# Get lst iterator and use nextAPI achieved manually iterative
# Use while simulate what iterator 
while 1 :
     the try :
        name = itr2. __next__ ()
         Print (name)
     the except StopIteration:
         BREAK 
# the try almost like fault tolerance in js


# Further in a method for determining whether the iterator 
from collections.abc Import the Iterable
 from collections.abc Import the Iterator

print(isinstance(itr,Iterable))
print(isinstance(itr,Iterator))

# True 
# True 
# two True so that is the iterator itr is iterables

print(isinstance(lst,Iterable))
print(isinstance(lst,Iterator))

# LST is iterable but not an iterator

 

Guess you like

Origin www.cnblogs.com/tengx/p/11671036.html