欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python单例模式示例

程序员文章站 2022-03-07 16:04:49
...
# -*- coding: utf-8 -*-
class SendMessage(object):
		"""调用第三方接口发送短信的功能"""
		
		def __new__(cls, *args, **kwargs):
				if not hasattr(cls, "_instance_"):
						cls._instance_ = super(SendMessage, cls).__new__(cls)
						cls.interface_url = "www.yangxiao1.com"
						print "第一次进来"
				return cls._instance_
		
		def send_msg(self, sim, msg):
				url = self.interface_url + "?msg=" + msg + "&phone=" + sim
				print url
				

s = SendMessage()
s.send_msg("110", "hello,110")

s2 = SendMessage()
s2.send_msg("120", "hello,120")

s3 = SendMessage()
s3.send_msg("119", "hello,119")
相关标签: 单例