The fifth chapter python learning function variables scope

Function
  function is a good combination, can be reused, or used to implement the code segment associated with a single function
effect
  can be improved and reuse of code modules of an application
to create a function of the
  rules of the first function
  1. Function block a beginning def keyword followed by the function identifier name and parentheses '()'
  2. All incoming parameters and arguments must be enclosed in parentheses, you can define the parameters in parentheses
  first statement 3. functions It can be used selectively documentation string, used for function description
  4.return [expression] end of the function, a selective return to the caller, return without an expression equivalent to return None

  syntax
    def function name (parameter list)
    function body
  call the function

  parameter of the function
  parameters Category: positional parameters, sequence parameters, keyword parameters, default parameters, keyword arguments dictionary, integrated mass participation
  parameter :( formal parameter)
    in def-defined function is the function name in brackets behind the variable into the form of parameter
  arguments:
    providing a variable called actual parameters or when a function call
1. Set transmission parameters
  defined:
    the actual parameters (arguments) of correspondence between the form of parameters (parameters) corresponding relationship is based on location a corresponding
  example:
    DEF Student (name, Age, Sex):
      Print ( 'name', name )
      print ( 'aged', Age)
      print ( 'Sex', Sex)
    Student ( 'Bob', 15, 'M')
  DESCRIPTION:
    sequence parameter passing, the sequence of dismantling position parameter correspondence with the
    location of the sequence parameter information corresponding to a respective position
  2. the sequence parameter passing
    sequence parameter passing cycle refers to the function call, with * the parameter passing sequence after transmission manner by dismantling position
  3. default parameters passed
    when calling a function, if not passed parameters, will use the default parameters
    using the default parameters, that is, when defining the function, parameter to a default value if no parameter assignment function to the calling function call will use the default value of this default
    Note:
      when default parameters when traditional values, the function is called when we pass the implementation of the value of
      the default parameter must be placed behind the non-default parameters
  parameters 4. keyword
    example:
      DEF Student (name, Age, Sex):
        Print ( 'name', name )
        Print ( 'aged', Age)
        Print ( 'results', score)
      Student ( 'Bob', Age = 15, = 20 is score)
  5. The parameter passing keyword dictionary
    definition:
      Argument refers to the dictionary, the dictionary with '**' dismantling keyword parameter passing after transmission parameters manner
    described:
      the dictionary of the keys and the parameter name must be the same
      dictionary must be a string key name (identifier)
      Dictionary of keys to be present in the parameter in
    the example:
    DEF Student (name, Age, Sex):
      Print ( 'name', name)
      Print ( 'Age', Age)
      Print ( 'achievement', score)
    # Student ( ' Bob ', Age = 15, Score = 20 is)
    D = {' name ':' Bob ',' Age ': 15,' Score ': 20 is}
    Stuident (** D)
  integrated parameter passing 6. the function
    definition:
      function the transmission parameters can be determined in a manner where the parameter can be uniquely matched to the corresponding argument in any combination can
    be described:
      a position parameter passing (SEQ pass parameters) to the parameter passing the left side of the keyword (keyword dictionary parameter passing) is
    def Myfun1 (A, B, C)
      Print (A, B, C)
    Myfun1 (100, * [200,300])
    Myfun1 (* (100,200), 300)
    Myfun1 (100, C = 300, B = 200 is)
    Myfun1 (100, ** ( 'c ': 300, 'b': 200))
  Function to
    1. There are no reference function and function parameter
      Example:
      Summary: variable in function of parameters can replace the contents, the contents can not immutable Alternatively parameter
    2. the function returns a value of the function and the return value does not
  exercise:
    write a function minmax (a, b, c) has three parameters, return these three parameters the minimum and maximum
    requirements, forming the tuple , the minimum value of the front, after the maximum

1 def minmax(a,b,c):
2     x=min(a,b,c)
3     y=max(a,b,c)
4     return x,y
5 dl=minmax(50,20,15)
6 print(dl)

  1. An asterisk tuple form
    syntax:
      def function name (* tuple parameter name)
        statement block
      or
      def function name (* args, name keyword parameter)
      Role:
        Force all parameters must
  2. Name the key shape reference
    syntax:
      def function name (* named key parameter),
    a block of statements
      or def function name (* args, name keyword parameter):
      role:
    Force all parameters must pass argument with a keyword or keyword dictionary parameter passing
  3. Double asterisks dictionary formed
    syntax:
      Def function name (parameter name dictionary **)
        statement block
    effect:
      collecting redundant keywords transmission parameters
    Description:
      dictionary parameter name is usually named 'kwargs'
  function of the parameters described
    position of the finger parameters, default parameters, the asterisk parameter tuples, double asterisk dictionary parameter can be mixed, the function parameter sequence from left to right are:
      a position parameter
      asterisk tuple parameter
      name keyword parameter
      double asterisk shape dictionary ginseng

    Variable length parameter function:
      asterisk tuples parameter, double asterisk dictionary parameter
      Description:
        can accept any position mass participation and keywords mass participation

    exercise:
      write a function similar to the built-in function max mymax
      See:
        >>> Help (max)
        fake mymax max write a function, the function is identical with max

 1 def mymax(a,*args):
 2     if len(args)==0:  #实参个数等于1
 3         zhemax=a[0]
 4         for x in a:
 5             if x>zhemax:
 6                 zhemax=x
 7         return zhemax
 8     else:
 9         zhemax=a
10         for x in args:
11             if x>zhemax:
12                 zhemax=x
13         return zhemax
14 print(mymax([6,8,35,5]))
15 print(mymax(100,200))

    Variable Scope
      Scope: namespace variables
      most basic scope of two variables: the local and global variables
    1. The local variables
      defined:
        1. The definition is called (local parameter is a function of local variables inside a function of variables variable)
        2. local variables can only be used within a function
        3. local variables can be created when the function is called, after the function call will be automatically destroyed
    2. global variables
      defined:
        the definition of the external, internal function block variable is called global variables
        global variables All functions can be accessed directly (single function can not be assigned)
    : 3. globals () / locals () function
      globals ()
      returns the current dictionary global scope variables of
      locals ()
      dictionary returns the current local scope variables
    4. function variable
      function name is a variable, a function when it binds def function statement creates
      a function may also be passed as an argument another function
      function can also be used as a further Returns the value of the number
    5. python scope
      scopes, also known name space, access the variable is a variable name to find the range space
      LEGB
      L local scope (the function)
      E scope external nested function
      G function definitions all modules (toplevel)
      B scope of the Python built-in module
    6. global statement
      1.global statement
      action
        1, the statement tells the interpreter actuator globa1 statement or more variables, for the scope of these variables scope module level, also referred to as global variables.
        2, global declarations (globa1) to map the variable assignment statements inside the module to operate file-scope
      grammar
        global variable 1, variable 2, ... ...
        Description
        1. If you want to be assigned global variables inside a function, you must after global declaration (otherwise it will be considered to create a local variable)
        2. global variables without declaration inside the function can be accessed directly (variable already exists and is associated with a target)
        3. can not declare a local variable, then the global variables declared as global this is not consistent with the provisions of
        4. globa1 variable list variable name can not appear in the parameter list for this scope in abnormal operation
      abnormal operation
        1. error definition and characteristics of
          what is wrong: wrong means, etc. Since the logic or grammar leads issue a program can not perform normal
          error characteristics: some errors can not predict
        2. Exceptional definition
          An exception is a program error is a state represented
          when an exception occurs, the program will not execute down, and turn to call this function in place pending the error and go back to the normal state

      Statement
        1.try:
          1.try-execpt statement
            try:
              may trigger the exception in
            execpt error type 1 [AS variable 1]
              Exception handling statements 1
            the except error type 2 [AS Variable 2]:
              Exception handling statements 2
            the except (error type 3 , error type 4, ... ...) [as variable 3]
              exception handling statements 3
            the except:
              exception handling statements (other)
            the else:
              no abnormality occurs statement
            finally:
              final statement
          effect of
            attempting to acquire abnormal, the abnormal state of the program and perform a normal state to normal
          example:
          the try - the except statement Description:
            variable 1. as a binding errors clause is the object, can be omitted
            2. except clause may have one or more, but at least a
            3. else clause can only be one and can be omitted
            4. finally clause can only be one and can be omitted
            5 . else clause of the statement will be executed when the try statement is not an exception occurs
            will 6. finally clause statement is executed in any case

Guess you like

Origin www.cnblogs.com/linxingh/p/11210765.html