Python _ Sequence and ing unpack operations, python sequence ing
UnpackEach element in the sequence or ing is extracted separately. A simple usage of sequence unpacking is to extract the first or first element and the following elements, for example:
First, seconde, * rest = sequence
If there are at least three elements in sequence, after executing the above Code, first = sequence [0], second = sequence [0], rest = sequence [2:].
Uncertain parameter received by function
When the parameter of the function is unknown, you can use * args and ** kwargs. * args has no key value, and ** kwargs has a key value.
#! /Usr/bin/python #-*-coding: UTF-8-*-import sysreload (sys) sys. setdefaultencoding ('utf-8') ''' when the parameter of the function is unknown, you can use * args and ** kwargs. * args has no key value, and ** kwargs has a key value. '''Def fun_var_args_kwargs (data1, * args, ** kwargs): print 'data1: ', type (data1), data1print' * args: ', type (args ), argsprint '** kwargs:', type (kwargs), kwargsfun_var_args_kwargs ('this is data1 ', 2, '3', 4.0, k1 = 'value1 ', k2 = 'value2') print '----------- 'def print_args (* args, ** kwargs): print args. _ class __. _ name __, args, kwargs. _ class __. _ name __, kwargsprint_args () print_args (1, 2, 3, a = 'A ')
Print result:
Data1: <type 'str'> this is data1
* Args: <type 'tuple'> (2, '3', 4.0)
** Kwargs: <type 'dict '> {'k2': 'value2', 'k1': 'value1 '}
-------------
Tuple () dict {}
Tuple (1, 2, 3) dict {'A': 'A '}
How does python unpack c except struct?
You can use cython for programming. In this way, the C language is used up and put in the PYTHON structure. In addition to struct, I can't think of any other methods.
Unpack python Parameters
Argv is a tuples. The first parameter is the script file name !! Therefore, you need to enter two parameters.
Filename, m, n = argv
Then 7.py 2 3
Then we can get m = 2, n = 3!