Python implements partial change method default parameter, pythonpartial
In the standard Python library, the functools library has many encapsulation functions for operations on methods. partial Objects is one of them, which can be used to modify the default values of method parameters. This article describes this function using the instance code.
Next let's take a look at a simple application test instance. The Code is as follows:
#! /Usr/bin/env python #-*-coding: UTF-8-*-# python2.7x # partial. py # authror: the Partial in orangleliu ''' functools can be used to change the default parameter of a method. 1. Change the default value of the original default parameter. 2. Add the default value '''def foo (, B = 0): '''int add''' print a + B # user default argumentfoo (1) # change default argument oncefoo (1, 1) # change function's default argument, and you can use the function with new argumentimport functoolsfoo1 = functools. partial (foo, B = 5) # change "B" default argumentfoo1 (1) foo2 = functools. partial (foo, a = 10) # give "a" default argumentfoo2 () ''' foo2 is a partial object, it only has three read-only attributesi will list them ''' print foo2.funcprint foo2.argsprint foo2.keywordsprint dir (foo2) # by default, the partial object does not have the _ name _ doc _ attribute, use update_wrapper to add attributes from the original method to the partial object. print foo2. _ doc _ ''' execution result: partial (func, * args, ** keywords) -new function with partial application of the given arguments and keywords. '''functools. update_wrapper (foo2, foo) print foo2. _ doc _ ''' modified to the foo Document Information '''
In this way, if we use a method that always requires several default parameters, we can first encapsulate it and don't need to set the same parameters every time.
I hope the methods described in this article will be of some reference and help value to everyone's Python program design.
In a Python User-Defined Function, what is = 0 after the parameter? ()
That is the default value. Note: If quantity is not assigned a value, the default value is 0, and the int class is also set.
An example of a python Program Calling a parameter is the kind of parameter-a-B added during runtime.
As 1L said, import the sys module, and then sys. argv is a list.
The content is all parameters.
Sys. argv [0] Is Your python script by default.
The following parameters are followed in sequence.
How can this problem be solved ......
Because no one will hurt a program specifically for parameter analysis, this step will not become the core function of a script
If you have to look at the example, you can read this article in my blog:
Blog.csdn.net/..584189
Mainly look at the starting point in the main function in the main program, it is processing the Parameter
I have also written a script with complicated processing parameters, but it cannot be released because it may involve some confidential content ...... Sorry ~