python vars()函数

1.python vars()定义
  1. 描述:vars() 函数返回对象object的属性和属性值的字典对象。
  2. 返回值: 返回对象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’)}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值