I. Function definition and BASIC application
#!/usr/bin/env Python3
‘‘‘
#函数的定义, the most basic application
Def myfirstfunction ():
A = 1+2
Print (a)
Myfirstfunction () #函数的执行
First = Myfirstfunction #函数可以指向一个对象
First ()
‘‘‘
‘‘‘
#函数的返回值 (return), we can determine whether the program executes successfully based on the return value, how the function does not use Retrun, and the return value is None
#邮件发送实例
#函数中遇到retrun值后, the code behind is not executing.
#retrun的作用: 1. Return value, 2. Terminal program
Import Smtplib
From Email.mime.text import Mimetext
From email.utils import formataddr
Def sendmails ():
RET = True
Try
msg = mimetext (' message content ', ' plain ', ' utf-8 ')
Msg[' from '] = FORMATADDR (["Du Junbung", ' [email protected] ')
msg[' to ' = Formataddr (["Ooxx", ' [email protected] ')
msg[' Subject ' = "Subject"
Server = Smtplib. SMTP ("smtp.163.com", 25)
Server.login ("[Email protected]", "Mabudai198")
Server.sendmail (' [email protected] ', [' [email protected] ',], msg.as_string ())
Server.quit ()
Except Exception:
RET = False
return ret
result = Sendmails ()
#print (Result)
If result:
Print (' Mail sent successfully ')
Else
Print (' message sent failed ')
‘‘‘
‘‘‘
#函数中遇到retrun值后, the code behind is not executing.
Def show ():
Print (' a ')
If 1==1:
return [+]
Print (' B ')
ret = Show ()
‘‘‘
Import Smtplib
From Email.mime.text import Mimetext
From email.utils import formataddr
#user是形式参数, Short form parameters
The parameters passed in #SendMails (' [email protected] ') are actual parameters, short arguments
def sendmails (user):
RET = True
Try
msg = mimetext (' message content ', ' plain ', ' utf-8 ')
msg[' from ' = Formataddr (["Name", ' [email protected] ')
msg[' to ' = Formataddr (["Ooxx", ' [email protected] ')
msg[' Subject ' = "Subject"
Server = Smtplib. SMTP ("smtp.163.com", 25)
Server.login ("[Email protected]", "password")
Server.sendmail (' [email protected] ', [user,], msg.as_string ())
Server.quit ()
Except Exception:
RET = False
return ret
result = Sendmails (' [email protected] ')
#print (Result)
If result:
Print (' Mail sent successfully ')
Else
Print (' message sent failed ')
Two. Parameters of the function
#!/usr/bin/env Python3
#函数中定义几个形参, you must pass in several arguments, or you will get an error.
#无参数
Def myfun ():
A = ' Hello baby '
Print (a)
Myfun ()
#一个参数
def Myfun2 (comm):
A = Comm
Print (a)
Myfun2 (' Hello Baby ')
#两个参数
def Myfun3 (S1,S2):
A = S1
b = s2
Print (A+B)
MYFUN3 (10,20)
The #默认参数 is to set a default value for the parameter, and if the argument is not passed at the time of the call, the default argument is called
#注意, the default argument must be placed at the end of the formal parameter
def Myfun4 (a,b=10):
c = A + b
Print (c)
MYFUN4 (10)
Myfun4 (10,20)
#指定参数, arguments can be passed in without order
Def Myfun5 (A, B):
Print (A, B)
Myfun5 (B=10,A=20)
#动态参数
#一个 * Indicates the conversion of all parameters passed into the tuple
def Myfun6 (*arg):
Print (Arg,type (ARG))
Myfun6 (a)
#两个 * Indicates the conversion of all parameters passed into the dictionary
#一个 * parameters are placed in front, two * in the back, to pass the argument to follow this rule
def Myfun7 (**arg):
Print (Arg,type (ARG))
Myfun7 (a=1,b=2,c=3)
#三个 * Indicates that all types of elements can be accepted
def Myfun8 (*args,**kwargs):
Print (Args,type (args))
Print (Kwargs,type (Kwargs))
#传值方法一:
MYFUN8 (1,2,4,a=1,b=2,c=3)
#传值方法二: When the element that is passed to "*args" is a list object, you must precede the object with an * number, and when the value of the element value Dictionary object for "**kwargs", you must precede the Dictionary object with two * numbers,
Flist = [11,22,33,44]
Dics = {' A ': ' Hello ', ' B ': ' Andy ', ' C ': ' Rain ', ' d ': ' Jack '}
MYFUN8 (*flist,**dics)
#如果不在对应的对象前面加 *, the incoming two objects are treated as "*args" values, elements of the tuple, and the value of "**kwargs" is null, that is, the dictionary
MYFUN8 (Flist,dics)
The definition and use of Python functions