1.python vars()定义
- 描述:vars() 函数返回对象object的属性和属性值的字典对象。
- 返回值: 返回对象object的属性和属性值的字典对象,如果没有参数,就打印当前调用位置的属性和属性值 类似 locals()。
2.vars()在发送邮件时运用的实例
class TofMessage(object):
PRIORITY_HIGH = '1'
def __init__(self):
self.Priority = self.PRIORITY_HIGH
def Build(self):
file = dict()
for k, v in vars(self).items():
if k != 'attachment':
file[k] = (None, v, 'text/plain; charset=utf-8')
print(11111, file)
return file
class TenMail(TofMessage):
URL = '/api/v1/Message/SendMail'
def __init__(self):
super(TofMessage, self).__init__()
self.EmailType = '1'
self.From = 'QuotedPrice@ten.com'
self.To = ''
self.Bcc = ''
self.CC = ''
self.Title = ''
self.Content = ''
self.BodyFormat = ''
self.attachment = []
class MessageHelper(object):
def send(self, message):
file = message.Build()
def send_mail(subject, body, mail_to=None, attachment=[], mail_cc=[], mail_bcc=[]):
msg = TenMail()
msg.Title = subject
msg.Content = body
msg.From = 'TCEe@tnt.com'
msg.To = ','.join(mail_to)
msg.CC = ','.join(mail_cc)
msg.Bcc = ','.join(mail_bcc)
msg.attachment = attachment
msghelper = MessageHelper()
msghelper.send(msg)
if __name__ == '__main__':
send_mail('22', '33', mail_to=['fsf'])
打印结果
{‘To’: (None, ‘fsf’, ‘text/plain; charset=utf-8’), ‘BodyFormat’: (None, ‘1’, ‘text/plain; charset=utf-8’),
‘Title’: (None, ‘22’, ‘text/plain; charset=utf-8’), ‘Bcc’: (None, ‘’, ‘text/plain; charset=utf-8’),
‘EmailType’: (None, ‘1’, ‘text/plain; charset=utf-8’), ‘CC’: (None, ‘’, ‘text/plain; charset=utf-8’),
‘From’: (None, ‘TCE@tencent.com’, ‘text/plain; charset=utf-8’),
‘Content’: (None, ‘33’, ‘text/plain; charset=utf-8’)}