Python imports directly into the non-fixed parameters of a function with a dictionary array

Source: Internet
Author: User

It was just the time to learn the logging module. The code is this:

Logging.basicconfig (filename= ' Test.log ', level=logging.                    DEBUG, format= '% (name) s% (asctime) s% (module) s-% (Lineno) d [% (levelname) s]:% (message) s ', datefmt= '%y-%m-%d%h:%m:%s ') logging.warning (' Test warning ') logging.info (' Test info ') logging.debug (' Test Debug ')

Above the Logging.basicconfig () to a bunch of parameters, and experiment, it seems necessary to use key parameters, not lazy. So curious to see the help

Import Logginghelp (logging.basicconfig) # below is the first 2 lines of help, followed by the Help on function basicconfig in module logging:basicconfig ( **kwargs) # It used to be a non-fixed parameter

Look, the whole is a non-fixed parameter, then only one of the honest one wrote. But when I think about it, the **kwargs will turn the key parameters into a dictionary, then the problem is:

Now that I'm going to become a dictionary, I'll just define a dictionary and pass in a dictionary.

Import logging# to define the parameters as a constant, put them in the configuration file or at the beginning of the file. In addition, format is too long to be written separately.              Log_format = '% (asctime) s% (module) s-% (Lineno) d [% (levelname) s]:% (message) s ' Log_kwargs = {' filename ': ' Test.log ', ' Level ': Logging. DEBUG, ' format ': Log_format, ' datefmt ': '%y-%m-%d%h:%m:%s '}logging.basicconfig (**log_kwargs) # Word Logging.warning (' Test warning ') logging.info (' Test info ') logging.debug (' Test Debug ') can be passed into the dictionary as a parameter before the code is added.

Now that the dictionary can pass the argument, the array is the same.

def deal_list (*args): print (args) def deal_dic (**kwargs): Print (Kwargs) deal_list (*[1,2,3,4,5]) # *args is to turn parameters into tuples, so here Print out a tuple deal_dic (**{' a ': 1, ' B ': 2})


Python imports directly into the non-fixed parameters of a function with a dictionary array

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.