Advanced Python -I function

Function called method in java.
  Tissue function is good, reusable, code segments used to implement a single, or the associated function.
  Function module of the application can be improved, and code reuse. You already know that Python provides many built-in functions, such as print ().
But you can also create your own functions, which are called user-defined functions.

Amendment 1, the supplemental file

  In fact the file is not modified, modify the file is actually, to back up the old files, and then modify the file backup, then delete the old file, the
backup file into the old file name, so it seems like a modification of the original the same file.
Also add that the operating mode of the file, not recommended mode with a '+' sign and write when he specifically wrote, when he read a special read!

1 with open('d:\orders.txt', mode='r', encoding='utf-8') as f1, \
2         open('d:\orders.txt.bak', mode='w', encoding='utf-8') as f2:
3     f1_content = f1.read()
4     f2.write(f1_content.replace('\n new content added', '\n new line'))
5 #Use os amended to delete the old file, rename the new file to the old file name, the statement must be placed with the outside, whether by error, the file being edited! 
. 6  Import OS
 . 7  
. 8 The os.remove ( ' D: \ Orders.txt ' )
 . 9 os.renames ( ' D: \ orders.txt.bak ' , ' D: \ Orders.txt ' )

2, def defined function: function sub built-in functions and custom functions, custom functions is here

def func():
    print('hello func')

3, calling function

func()

4, the return value of the function return

Three cases the return value
  is no return value - returns None
    not return to write
    write only return: continue beyond the end of a function
    return None - less common
  return a value
    may return any data type
    as long as it can receive a return
    if a program there are a plurality of return, then only the implementation of a
  return value of a plurality of
    receiving a plurality of variables: the number of receiving the return value variable number
    with a received variable: a tuple is obtained

1  # 1) using a return to exit the function, the function behind the statements are not executed, corresponding to BREAK 
2  DEF func_return ():
 . 3      Print ( ' func_return ' )
 . 4      return   # may take None 
. 5      Print ( ' haha ' )
 . 6  func_return ()
 . 7  # 2) returns the value 
. 8  DEF func_return_s ():
 . 9      return [. 1, 2,. 3 ]
 10  
. 11  Print (func_return_s ())

5, parameters of the function

1), parameter: the position parameter, the variable parameter (* args), the default parameters of the variable keyword parameter (** kargs) 
2), actual parameters: transmission parameters according to the position, according to a keyword parameter passing
1  DEF func_have_arg (Arg):   # Arg here is in the form of parameters, i.e. the parameter 
2      Print ( ' you pass parameters S% ' % Arg)
 . 3 func_have_arg (1)   # 1 herein is the actual parameters, i.e., argument 
. 4  
. 5  DEF func_have_args (arg1, arg2):
 . 6      Print ( " you are passed the parameter: {0} - {1} ' .format (arg1, arg2))
 . 7 func_have_args ( " parameter 1 " , " parameter 2 ' )
 . 8  
. 9  DEF func_have_variable_args (* args): # variable parameters passed to the function in the form of a tuple
10      Print (args)
 . 11 func_have_variable_args (1, 2 )
 12 is func_have_variable_args (1, 2, ' parameter 1 " , " parameter 2 " )
 13 is  
14  DEF func_have_default_value (arg1, arg2 = ' MALE ' ):   # default order parameter In an after varying parameter 
15      Print ( " containing default parameters, the input parameters can not use the default values, input will override the default values 0} {-! {}. 1 " .format (arg1, arg2))
 16 func_have_default_value ( ' sex ' )
 . 17 func_have_default_value ( ' sex' , ' FEMALE ' )
 18 is  
. 19  DEF func_have_keyword_variable_args (** kwargs):   # variable keyword parameter 
20 is      Print ( ' variable keyword parameter is: {0} ' .format (kwargs))
 21 is func_have_keyword_variable_args (name = ' Tom ' , Ginder = " FEMALE " )
View Code
3), the dynamic parameter passing another way
DEF func_dy (* args): # standing on the angle parameter, coupled to the variable *, is a combination of all the values coming. 
    Print (args)

func_dy ( 1,2,3,4,5 )
l = [1,2,3,4,5]
func_dy ( * L)   # standing on the angle of the argument, a sequence to add *, this sequence is broken up in the order of 
# func_dy (** L) can be derived according to the key way of reference passed variable parameters

parameter

    No parameters 
when defining the function and calling the function in brackets do not write the contents of
a parameter
of what passed is what
multiple parameters
positional parameters

standing on the angle of the argument:
a reference position in accordance pass
by keyword mass participation
Hunzhe can use: it must be according to the location parameter passing, and then pass the parameter by keyword
can not pass the same to a plurality of variable values

on the parameter angles standing
position parameter: must pass, and there are several parameters to pass several values
default parameters: can not pass if you do not pass is to use the default parameter, if passed on a pass

when only call functions
in accordance with the position of the pass: write parameter values directly
by keyword: keyword = value

defined function of time:
positional parameters: direct-defined parameters
default parameters, keyword parameters: parameter name = 'default value'
can accept any number of parameters: the dynamic parameter
applied before * parameter name, parameter names customary args,
before adding ** parameter name, parameter names kwargs customary
sequence: position parameter , * args, default parameters, ** kwargs

dynamic parameters have two : Can take any arguments
* args: in accordance with the received value of parameter passing positions, organized into a tuple
** kwargs: accepted by keyword parameter passing values, organized as a dictionary
before it must kwargs args

6, the comment function

def func_comment(args):
    '''
    Presentation function annotations
    : Param args: variable parameters
    :return: None
    '''
    return None

 

 

Guess you like

Origin www.cnblogs.com/funyou/p/11921819.html