Recursive, decorators, Python common built-in method

Recursive ** **
        DEF Calc (n-):
            Print (n-)
            IF int (n-/ 2) == 0: conditional
                return n-
            return Calc (int (n-/ 2))
        Calc (10)
        result 10,5, 2,1
        
        next process is to achieve a
            DEF Calc (n-):
                Print (n-)
                n-int = (n-/ 2)
                IF n-> 0:
                    Calc (n-)
                Print (n-)
            Calc (10)
        the result is 10, 5 1, 0,1,2,5
        (implementation process, recursion will go out a few times)
        
        
    inside the function can call other functions, if a function call inside the function itself,
        this function is a recursive function.
        1, must have a clear end condition
        2, each recursive, have reduced the scale of the problem
        3, recursive efficiency is not high (stack overflow)
        
            = A '. 1. 4. 3. 6. 7. 8. 9. 11. 17. 19 15 25 29 21 is 22 is 33 is 38 is 69 107'
            B = a.split ()
            C = []
            for B in I:
                c.append (int (I)) to # list of strings into a digital number list
            Print (C)
            # Print (len (C))
            D = [X 2 for X in Range ** (11,100)]
            Print (D)
            c.extend (D)
            E = C
            Print ( C)
            Print (E)
            # Print (ID (C))
            # Print (ID (E))

            DEF binary_search (DataSet, find_num):
                Print (DataSet)
                IF len (DataSet)>. 1: length # list
                    if find_num <dataset [-1]:
                        // length list # 2 mid = len (dataset) half
                        IF DataSet [MID] == find_num:
                            Print ( 'IT Find', find_num)
                        elif DataSet [MID] <find_num:
                            Print ( 'Get the number of the list after half ')
                            # DataSet = DataSet [MID +. 1:]
                            return binary_search (DataSet [MID +. 1:], find_num)
                        the else:
                            Print (' are looking for numbers at the front half of the list ')
                            # DataSet = DataSet [: MID ]
                            return binary_search (DataSet [: MID], find_num)
                    the else:
                        Print ( 'The number outside the list, this number is greater than the number of all of this list ')
                the else:
                    IF DataSet [0] == find_num:
                        Print ( 'only one element', find_num)
                    the else:
                        Print ( 'element is not in the list to find a')


            binary_search (E, 100)
            
            
** built-in function **
    HTTPS: // book.apeland.cn/media/images/2019/03/21/chapter3-built-in.png
    
    built arguments detailed https://docs.python.org/3/library/functions.html?highlight=built#ascii
    
    a str or bytes type can not directly change, bytearray () method of the result becomes list, you can change the
    
    isinstance () to determine the type of a data structure of the
    role of each function of the standard I help you better

    abs # absolute value

    all # return True if bool (x) is True for all values x in the iterable.If the iterable is empty, return True.

    IF BOOL True #Return the any (the X-) in the X-IS True for the any Iterable at The iterable.If at The IS empty, return False.

    ASCII #Return AN AN ASCII-only representation of Object, ASCII ( "China") returns " '\ u4e2d \ u56fd ' "

    bin # 2 returns the integer notation

    bool # determines a data structure is True or False, bool ({} ) that returns False, because it is empty dict

    ByteArray ByteArray # into the byte, the array can be modified

    bytes # bytes ( "China", "GBK")

    a callable # determine whether an object can be called

    chr # returns ascii characters in a number corresponding, for example, chr (90) returns the ascii inside 'the Z-'

    classmethod # object-oriented when used, are now ignored

    compile #py interpreter themselves with things, ignoring

    complex # seek the complex, it is generally less than

    copyright # useless

    credits # useless

    delattr # when using object-oriented, ignoring now

    dict # creates an empty dict

    dir # returns an object can be called Attributes

    divmod # Returns the quotient and remainder of the division, such divmod (4,2), the result (2, 0)

    after enumerate # and returns a list of the index element, such as d = [ "alex", " jack"], enumerate (d) to give (0, 'Alex') (. 1, 'Jack')

    the eval # string may be in the form of list, dict, set, tuple, which is converted to the original data type.

    exec # format code string, and performing de-sense, such as exec ( "print ( 'hellworld' )"), which will Explanations string and performing

    exit # exit the program

    filter # on the list, dict, set, tuple etc. iterables filter, filter (lambda x: x> 10, [0,1,23,3,4,4,5,6,67,7]) filter out all values greater than 10

    float # transformed into floating point

    format # useless

    frozenset # to become a collection of non-modifiable

    getattr # when using object-oriented, now ignored

    globals # prints global scope in the value

    hasattr # when using object-oriented, and now ignore

    hash #hash function

    Help

    hex # returns a decimal hexadecimal representation, hex (10) to return '0xA'

    ID # view object memory address

    INPUT

    int

    isinstance # determines a type of data structure, such a determination is not fronzenset, isinstance (a, frozenset) returns False or True

    issubclass # with an object-oriented, now ignoring

    iter # put into a data structure iterator, spoke iterator I understand

    len

    List

    about locals

    Map Map # (the lambda X: X ** 2, [1,2,3,43,45,5,6,]) output [1, 4, 9, 1849, 2025, 25, 36 ]

    max # seeking maximum

    memoryview # most people do not, ignoring

    min # minimization

    next # generators will be used, it is ignored

    when using object-oriented object #, now ignoring

    oct # 8 returns the decimal number hexadecimal

    Open

    the ord # returns the decimal ord ( 'a') corresponds to ascii characters return 97,

    Print

    when using the object-oriented property #, now ignores

    quit

    Range

    the repr # nothing with

    the reversed # inversion can be a list of

    round # 4 can be fractional 5 rounded into integers, round (10.15,1) to give 10.2

    SET

    setattr # with an object-oriented, now ignoring

    slice # useless

    the sorted

    staticmethod # with an object-oriented, now ignoring

    STR

    SUM # summation, a = [1, 4, 9, 1849, 2025, 25, 36], sum ( a) to give 3949

    Super # with an object-oriented, now ignoring

    tuple

    type

    VARS # returns the attribute of an object, an object-oriented understand

    zip # 2 or more can makes up a list, a = [1, 4, . 9, 1849, 2025, 25, 36], B = [ "A", "B", "C", "D"],

    List (ZIP (A, B)) # results obtained
    [(1, 'a' ), (. 4, 'B'), (. 9, 'C'), (1849, 'D')]
    

** ** namespaces: a local storage binding relationship with the name x (x = 1)
    namespace there are four: LEGB
        . 1, about locals: internal name of the function space, generally includes a function of local variables and parameters in the form of
        2, enclosing function: the function name in the space of the outer nested function, if fun2 fun1 nested in,
        for fun2 speaking, fun1 namespace is enclosing.
        3, globals: the current space module, the module is that some py file, that is to say, globals () is similar to global variables.
        4, __ builtins__: built-in module space, which is built or built-in functions namespace,
        Print (dir (__ builtins__)) may see the value it contains.
        
    Different scopes of different variables is the name of the variable space where the decision.
            a. Global scope: global survival, globally effective
            b. Local scope: Temporary survival, local effective
        viewing scopes way: Globals (), about locals ()
LEGB on behalf of name lookup order: locals-> enclosing function-> Globals -> __ builtins__


** closure **

    DEF Outer ():
        name = 'circle brassicae, self-programming'
    
        DEF inner ():
            Print ( 'Inner', name)
            
        return inner
        
    FUNC = Outer () # returns the memory address of inner, inner
    FUNC () # is equivalent to the implementation of the inner ( )
    
    significance closures: the function returns an object, not just a function object, outside the function
                also wrapped in a layer of the scope, so that, regardless of where the function call,
                Priority use of outer wrap their scope.
                
                
** decorator acquaintance ** (open - closed principle)
        to change the way call (Starter Edition):
        the Account = {
            'is_authenticated': False, # user logged in to put this change True
            'username': 'alex' , # pretend it DB in stored user information
            'password': 'abc123' # pretend it DB in stored user information
        }
        DEF Login (FUNC):
            IF Account [ 'is_authenticated'] iS False:
                username = INPUT ( ' User: '.) Strip ()
                password = INPUT (' password: '.) Strip ()
                IF Account username == [' username '] == Account and password [' password ']:
                    Print (' Login available for purchase ... . ')
                    Account ['
                    func () # authentication is successful, perform the functions of the function
                the else:
                    Print ( 'Wrong username or password')
            the else:
                Print ( 'user is logged, verified by')
                func () # authentication is successful, the implementation of performance function
        DEF Home ():
            Print ( 'the HomePage --- ---')
        DEF Ameica ():
            #login () # before performing verification plus
            Print ( '--- ---- am_area')
        DEF japan ():
            Print ( '--- Japan and South Korea Area --- ')
        DEF henan ():
            Print (' ----- Henan Area ----- ')
        Home ()
        the Login (Ameica)
        the Login (henan)
        
        
        
        does not change the call mode (Starter Edition) :
        Account = {
            'is_authenticated': False, # user logged in to put this change True
            'username': 'alex', # pretend it is stored in the user information DB
            'password': 'abc123' # pretend it is stored in the user DB information
        }
        DEF Login (FUNC):
            DEF Inner (* args, ** kwargs):
                IF Account [ 'is_authenticated'] IS False:
                    username = INPUT ( 'User:'). Strip ()
                    password = INPUT ( 'password:' ) .strip ()
                    IF Account username == [ 'username'] == Account and password [ 'password']:
                        Print ( 'Login available for purchase ....')
                        Account [ 'is_authenticated'] = True
                        FUNC (* args, ** kwargs) # authentication is successful, the implementation of performance function
                    the else:
                        Print ( 'Wrong username or password')
                the else:
                    Print ( 'user is logged, verified by')
                    FUNC (* args, ** kwargs) # authentication is successful, perform the functions of the function
            return Inner
        DEF Home ():
            Print ( ' the HomePage --- --- ')
        @login # = Login equivalent Ameica (Ameica)
        DEF Ameica ():
            #login () # authentication before performing plus
            Print (' --- ---- am_area ')
        DEF Japan ():
            Print ( '--- CJK zone ---')
        # @ Pay
        @login equivalent to # henan = login (henan) decorator, syntactic sugar
        DEF Henan (vip_level):
            IF vip_level>. 3:
                print ( 'unlock all senior games are played in this area')
            the else:
                print ( '----- ----- Henan area vip')
        # Home ()
        # the Login (Ameica)
        # the Login (henan)
        Ameica = the Login (Ameica ) # inner memory address
        henan = login (henan) #inner memory address
        Home ()
        Ameica () # inner ()
        Henan (. 4) #inner ()






































































































            
       

Guess you like

Origin www.cnblogs.com/ylkx/p/11228892.html