Python programming -------- Functions

Source: Internet
Author: User

Python programming -------- Functions
I. Function programming: sending emails #! /Usr/bin/env python #-*-coding: UTF-8-*-import smtplibfrom email. mime. text import MIMETextfrom email. utils import formataddr def mails (): msg = MIMEText ('mail content', 'plain ', 'utf-8 ') msg ['from'] = formataddr (["Python fans", 'zypython @ 126.com ']) msg ['to'] = formataddr (["From Python Official Website ", '6666633xxx @ qq.com ']) msg ['subobject'] = "topic" server = smtplib. SMTP ("smtp.126.com", 25) server. the login ("zypython@126.com", "password") server. sendmail ('zypython @ 126.com ', ['6666633xxx @ qq.com',], msg. as_string () server. knowledge points of quit () mails () functions: 1. Keywords of def-defined functions 2. function names, which are called later by function names 3. Function declarations, which are not automatically executed; after the call, execute 4. function parameter 5. function return value 1. function return value: 1. If the function body does not define the return value Action statement, the result returned by the function is none by default. 2. If a return value is returned, the return value can be assigned to a variable. 2. Common parameter of the function: Common parameter default parameter dynamic parameter common parameter: form parameters (parameters) and actual parameters (real parameters) # form parameters (parameters) # The actual parameters (real parameters) can have N parameters, and the input of a specified number of parameters has the following function email, the message in the brackets is a formal parameter, which can be defined at will. When a function is called, the input parameter is the actual parameter, that is, the real parameter ("CPU alarm ")) def email (message): msg = MIMEText ('message', 'plain ', 'utf-8') msg ['from'] = formataddr (["python enthusiast ", 'zhangyun @ opark.com ']) msg ['to'] = formataddr (["leaving", '2017 @ qq.com']) msg ['subobject'] = "topic" server = smtplib. SMTP ("smtp.qq.com", 25) server. login ("zhangyun@opark.com", "roots @ 123") server. sendmail ('zhangyun @ opark.com ', ['2017 @ qq.com',], msg. as_string () server. quit () email ("CPU alarm ") 3. default parameters of functions 1. default values are used if values are not passed 2. default parameters must be placed at the end of the parameter list 3. default parameters can also have multiple examples: def email (message, subject = "Monitoring Alarm System"): msg = MIMEText ('message', 'plain ', 'utf-8 ') msg ['from'] = formataddr (["python fans", 'zhangyun @ opark.com ']) msg ['to'] = formataddr (["quit ", '1970 @ qq.com ']) msg ['subobject'] = "topic" server = smtplib. SMTP ("smtp.qq.com", 25) server. login ("zhangyun@opark.com", "roots @ 123") server. sendmail ('zhangyun @ opark.com ', ['2017 @ qq.com',], msg. as_string () server. quit () email ("CPU alarm", subject = "Monitoring Alarm System") email ("Hard Disk alarm", subject = "production business system") email ("CPU alarm ") in the above function email (message, subject = "Monitoring Alarm System") parameter, subject = "Monitoring Alarm System" is the default parameter, which is placed at the end of the call and the default parameter at the end, that is, email ("CPU alarm", subject = "Monitoring Alarm System"); you can also initialize the default parameter: email ("Hard Disk alarm", subject = "production business system "), in this case, the value of the subject defined in the original function will be overwritten by the "production business system". When calling the function, the default parameter does not need to be passed: email ("CPU alarm ") iv. Dynamic Parameters (1) (input single value to construct a metagroup) def func (* args ): pass1: receives multiple parameters. 2. Automatically constructs tuples internally. 3. If a wear sequence is prefixed with a star number (*), the tuples can be avoided, the output sample code is as follows: li_list = [11,22, 33,44, 55] li_tuple = ('A', 'B', 'C', 'D ') def func (* args): print args print "######## here is the split line #####" func (123) func ("abdcdefg ") func (li_list) func (li_tuple) func (* li_list) func (* li_tuple) output result: (123 ,) ######## split line ##### ('abdcdef ',) ######## split line ##### ([11, 22, 33, 44, 55],) ######## split line ##### ('A', 'B', 'C', 'D '),) ######## split line ##### (11, 22, 33, 44, 55) ######## split line ##### ('A', 'B', 'C', 'D ') ######## here is the split line ##### if you want to obtain the value of the input parameter, you can use the subscript of the parameter to obtain it. The sample code is as follows: li_list = [11, 22, 33,44, 55] li_tuple = ('A', 'B', 'C', 'D') def func (* args ): print args [0] print "######## split line ####" func (123) func ("abdcdefg") func (li_list) func (li_tuple) func (* li_list) func (* li_tuple) 123 ######## here is the split line ##### abdcdefg ######## here is the split line ##### [11, 22, 33, 44, 55] ####### split line #### ('A', 'B', 'C', 'D ') ######## here is the split line ##### 11 ######## here is the split line ##### ###### # split line ##### 4. Dynamic Parameters (2) (The input parameter is the data structure of the dictionary.) The sample code is as follows: dic = {'k1': 123, 'k2': 321, 'k3 ': 431} def func (** kargs): print kargs print "###### here is the split line ######" func (k1 = 123) func (k2 = 321) func (** dic) output result: {'k1 ': 123 }###### here is the split line ###### {'k2 ': 321 ####### here is the split line ###### {'k3': 431, 'k2': 321, 'k1 ': 123 }####### here is the split line ##### 5. Dynamic functions (3) (both single and double values are input to construct a metagroup and dictionary respectively) def func (* args, ** kwargs ): print args print kwargs print "######### here is the split line ########" func (123, 33) func (k1 =, k2 = 999) func (123, 33, k1 = 999, k2 =) output result: (11, 22, 33) {}######### here is the split line ######### () {'k2': 999, 'k1 ': 123 }######### split line ######### (11, 22, 33) {'k2': 999, 'k1': 123 }######### split line #########

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.