First, the function of the programming of the sending of e-mail
#!/usr/bin/env python
#-*-Coding:utf-8-*-
Import Smtplib
From Email.mime.text import Mimetext
From email.utils import formataddr
def mails ():
msg = mimetext (' message content ', ' plain ', ' utf-8 ')
msg[' from ' = Formataddr (["Python Enthusiast", ' [email protected] ')
msg[' to ' = Formataddr (["From Python website", ' [email protected] ')
msg[' Subject ' = "Subject"
Server = Smtplib. SMTP ("smtp.126.com", 25)
Server.login ("[Email protected]", "password")
Server.sendmail (' [email protected] ', [' [email protected] ',], msg.as_string ())
Server.quit ()
Mails ()
Related knowledge points of the function:
1. def defines the keyword of the function
2, function name, followed by function name call the function
3, function declaration, not automatic execution;
4. Parameters of the function
5. return value of function
First, the return value of the function:
1, if the function body does not define a return value action statement, then the function default returns the result is None
2, if there is a return value can be assigned to a variable value
General parameters of the function
Parameters:
General parameters
Default parameters
Dynamic parameters
Common parameters: Formal parameters (formal arguments) and actual parameters (arguments)
#形式参数 (Formal parameters)
#实际参数 (actual parameter)
parameter can have n and pass in the specified number of arguments
There is the following function e-mail, where the message in parentheses is a formal parameter, formal parameters can be arbitrarily defined, and call the function when the parameters passed in as the actual parameters, that is, the argument ("CPU alarm")
def email (message):
msg = mimetext (' message ', ' plain ', ' utf-8 ')
msg[' from ' = Formataddr (["Python Enthusiast", ' [email protected] ')
msg[' to ' = Formataddr (["Leave", ' [email protected] ')
msg[' Subject ' = "Subject"
Server = Smtplib. SMTP ("smtp.qq.com", 25)
Server.login ("[Email protected]", "[email protected]")
Server.sendmail (' [email protected] ', [' [email protected] ',], msg.as_string ())
Server.quit ()
Email ("CPU alarm")
Third, the default parameters of the function
1, do not pass the value of the default value
2. The default parameter must be placed at the end of the parameter list
3, the default parameters can also have multiple
Example:
def email (message,subject= "Monitoring Alarm system"):
msg = mimetext (' message ', ' plain ', ' utf-8 ')
msg[' from ' = Formataddr (["Python Enthusiast", ' [email protected] ')
msg[' to ' = Formataddr (["Leave", ' [email protected] ')
msg[' Subject ' = "Subject"
Server = Smtplib. SMTP ("smtp.qq.com", 25)
Server.login ("[Email protected]", "[email protected]")
Server.sendmail (' [email protected] ', [' [email protected] ',], msg.as_string ())
Server.quit ()
Email ("CPU alarm", subject= "Monitoring alarm system")
Email ("hdd Alarm", subject= "production business System")
Email ("CPU alarm")
The above function email (message,subject= "Monitoring Alarm system") parameters subject= "Monitoring alarm system" as the default parameters, in the call, put in the last, the default parameters in the end, that is, email ("CPU alarm", subject= "Monitoring alarm system" ) or initialize default parameters: Email ("hdd Alarm", subject= "production business System"), the value of the subject defined in the original function will be overwritten by the "Production business system"; The default parameter can also be used without a value when calling a function: email ("CPU alarm")
Four, dynamic parameters (one) (incoming single-value construction Narimoto Group)
def func (*args):
Pass
1. Receive multiple parameters
2, internal automatic construction tuple
3, if the sequence of wear, before the sequence asterisking (*), you can avoid the re-construction of the tuple, that is, what to wear in the output of what
The sample code is as follows:
Li_list = [11,22,33,44,55]
Li_tuple = (' A ', ' B ', ' C ', ' d ')
def func (*args):
Print args
Print "####### #这里是分割线 #####"
Func (123)
Func ("ABDCDEFG")
Func (Li_list)
Func (Li_tuple)
Func (*li_list)
Func (*li_tuple)
Output Result:
(123,)
####### #这里是分割线 #####
(' ABDCDEFG ',)
####### #这里是分割线 #####
([11, 22, 33, 44, 55],)
####### #这里是分割线 #####
(' A ', ' B ', ' C ', ' d '),
####### #这里是分割线 #####
(11, 22, 33, 44, 55)
####### #这里是分割线 #####
(' A ', ' B ', ' C ', ' d ')
####### #这里是分割线 #####
If you want to get the value of an incoming parameter, you can get it by the parameter subscript:
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 "####### #这里是分割线 #####"
Func (123)
Func ("ABDCDEFG")
Func (Li_list)
Func (Li_tuple)
Func (*li_list)
Func (*li_tuple)
123
####### #这里是分割线 #####
Abdcdefg
####### #这里是分割线 #####
[11, 22, 33, 44, 55]
####### #这里是分割线 #####
(' A ', ' B ', ' C ', ' d ')
####### #这里是分割线 #####
11
####### #这里是分割线 #####
A
####### #这里是分割线 #####
Iv. Dynamic Parameters (ii) (data structure of the input parameter is a dictionary)
The sample code is as follows:
DiC ={' K1 ': 123, ' K2 ': 321, ' K3 ': 431}
def func (**kargs):
Print Kargs
Print "##### #这里是分割线 ######"
Func (k1=123)
Func (k2=321)
Func (**dic)
Output Result:
{' K1 ': 123}
##### #这里是分割线 ######
{' K2 ': 321}
##### #这里是分割线 ######
{' K3 ': 431, ' K2 ': 321, ' K1 ': 123}
##### #这里是分割线 ######
Five, dynamic function (three) (simultaneously passing in single and double values, constructing Narimoto groups and dictionaries, respectively)
def func (*args,**kwargs):
Print args
Print Kwargs
Print "######## #这里是分割线 #########"
Func (11,22,33)
Func (k1=123,k2=999)
Func (11,22,33,k1=123,k2=999)
Output Result:
(11, 22, 33)
{}
######## #这里是分割线 #########
()
{' K2 ': 999, ' K1 ': 123}
######## #这里是分割线 #########
(11, 22, 33)
{' K2 ': 999, ' K1 ': 123}
######## #这里是分割线 #########
This article from "Flat Light is true" blog, please be sure to keep this source http://cryan.blog.51cto.com/10837891/1718692
--------functions of Python programming