Python sequence and map unpacking operations

Source: Internet
Author: User
Unpacking is to extract each element of a sequence or map separately, and a simple use of a sequence unpacking is to extract the first or previous elements separately from the following elements, for example:

First, seconde, *rest = sequence

If there are at least three elements in the sequence, then after executing the above code, first = = Sequence[0], second = = Sequence[0], rest = = sequence[2:].

function receives indeterminate parameter

When the parameters of a function are indeterminate, you can use *args and **kwargs,*args without a key value, **kwargs has a key value.

#!/usr/bin/python#-*-coding:utf-8-*-import sysreload (SYS) sys.setdefaultencoding (' utf-8 ') '  when the parameters of the function are indeterminate, you can use the *args and **kwargs,*args do not have a key value, **kwargs has a key value.  "  def fun_var_args_kwargs (Data1, *args, **kwargs):    print ' data1: ', type (data1), data1    print ' *args : ', type (args), args    print ' **kwargs: ', type (Kwargs), Kwargs  Fun_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__, Kwargs  Print_args () Print_args (1, 2, 3, a= ' a ')

Printing results:

Data1: This is 
 
  
   
   data1*args: 
  
   
    
    (2, ' 3 ', 4.0) **kwargs: 
   
    
     
     {' K2 ': ' value2 ', ' K1 ': ' Value1 '}-------------tuple () dict {}tuple (1, 2, 3) dict {' A ': ' A '}
   
    
  
   
 
  
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.