python 實(shí)現(xiàn)單例模式的5種方法
一、classmethod裝飾器
# 全局變量
ip = '192.168.13.98'
port = '3306'
class MySQL:
__instance = None
def __init__(self, ip, port):
self.ip = ip
self.port = port
@classmethod
def instance(cls, *args, **kwargs):
if args or kwargs:
cls.__instance = cls(*args, **kwargs)
return cls.__instance
obj1 = MySQL.instance(ip, port)
obj2 = MySQL.instance()
obj3 = MySQL.instance()
print(obj1)
print(obj2, obj2.__dict__)
print(obj3, obj3.__dict__)
輸出結(jié)果
<main.MySQL object at 0x058D6F30>
<main.MySQL object at 0x058D6F30> {'ip': '192.168.13.98', 'port': '3306'}
<main.MySQL object at 0x058D6F30> {'ip': '192.168.13.98', 'port': '3306'}
二、類的裝飾器
def singlegon(cls):
_instance = cls(ip, port)
def wrapper(*args, **kwargs):
if args or kwargs:
return cls(*args, **kwargs)
return _instance
return wrapper
@singlegon
class MySQL1:
def __init__(self, ip, port):
self.ip = ip
self.port = port
obj1 = MySQL1()
obj2 = MySQL1()
obj3 = MySQL1('1.1.1.3', 8080)
print(obj1)
print(obj2, obj2.__dict__)
print(obj3, obj3.__dict__)
運(yùn)行結(jié)果
<main.MySQL1 object at 0x04C102B0>
<main.MySQL1 object at 0x04C102B0> {'ip': '192.168.13.98', 'port': '3306'}
<main.MySQL1 object at 0x04C10310> {'ip': '1.1.1.3', 'port': 8080}
三、元類
class Mymetaclass(type):
def __init__(self, class_name, class_bases, class_dic):
super().__init__(class_name, class_bases, class_dic)
self.__instance = self(ip, port)
def __call__(self, *args, **kwargs):
if args or kwargs:
obj = self.__new__(self)
self.__init__(obj, *args, **kwargs)
self.__instance = obj
return self.__instance
class MySQL2(metaclass=Mymetaclass):
def __init__(self, ip, port):
self.ip = ip
self.port = port
obj1 = MySQL2()
obj2 = MySQL2()
obj3 = MySQL2('1.1.1.3', 80)
print(obj1)
print(obj2, obj2.__dict__)
print(obj3, obj3.__dict__)
運(yùn)行結(jié)果
<main.MySQL2 object at 0x04D003B0>
<main.MySQL2 object at 0x04D003B0> {'ip': '192.168.13.98', 'port': '3306'}
<main.MySQL2 object at 0x04D003D0> {'ip': '1.1.1.3', 'port': 80}
四、模塊導(dǎo)入
# instance.py
class MySQL:
def __init__(self, ip, port):
self.ip = ip
self.port = port
ip = '192.168.13.98'
port = 3306
instance = MySQL(ip, port)
# 測(cè)試代碼
import os, sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from test import instance
obj1 = instance.instance
obj2 = instance.instance
obj3 = instance.MySQL('1.1.1.3', 80)
print(obj1)
print(obj2, obj2.__dict__)
print(obj3, obj3.__dict__)
運(yùn)行結(jié)果
<day30.instance.MySQL object at 0x052B0AB0>
<day30.instance.MySQL object at 0x052B0AB0> {'ip': '192.168.13.98', 'port': 3306}
<day30.instance.MySQL object at 0x052B03F0> {'ip': '1.1.1.3', 'port': 80}
五、重寫(xiě)__new__()
class MySQL3(object):
__instance = None
__first_init = True
def __init__(self, ip, port):
if self.__first_init:
self.ip = ip
self.port = port
self.__first_init = False
def __new__(cls, *args, **kwargs):
if not cls.__instance:
cls.__instance = object.__new__(cls)
return cls.__instance
obj1 = MySQL3(ip, port)
obj2 = MySQL3(ip, port)
obj3 = MySQL3('1.1.1.3', 80)
print(obj1)
print(obj2, obj2.__dict__)
print(obj3, obj3.__dict__)
運(yùn)行結(jié)果
<main.MySQL3 object at 0x059603F0>
<main.MySQL3 object at 0x059603F0> {'ip': '192.168.13.98', 'port': '3306', '_MySQL3__first_init': False}
<main.MySQL3 object at 0x059603F0> {'ip': '192.168.13.98', 'port': '3306', '_MySQL3__first_init': False}
注:前四種可以實(shí)現(xiàn)單例模式,但都不是絕對(duì)單例模式,可以創(chuàng)建新的對(duì)象,但是第五種方式是絕對(duì)單例模式,全局只能真正創(chuàng)建一次對(duì)象
以上就是python 實(shí)現(xiàn)單例模式的5種方法的詳細(xì)內(nèi)容,更多關(guān)于python 單例模式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python實(shí)現(xiàn)跨年表白神器--你值得擁有
這篇文章主要介紹了python實(shí)現(xiàn)跨年表白神器的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-01-01
Python利用zhconv模塊進(jìn)行簡(jiǎn)繁體字轉(zhuǎn)換的案例演示
zhconv是一個(gè)Python庫(kù),提供了簡(jiǎn)體字和繁體字之間的轉(zhuǎn)換功能,本教程將向你展示如何使用zhconv模塊來(lái)實(shí)現(xiàn)簡(jiǎn)繁體字的互轉(zhuǎn),并附帶一個(gè)案例演示,感興趣的朋友可以參考一下2024-05-05
Python函數(shù)默認(rèn)返回None的原因及分析
Python函數(shù)默認(rèn)返回None是因?yàn)樵谡Z(yǔ)法層面,解釋器會(huì)主動(dòng)地為沒(méi)有return語(yǔ)句的函數(shù)添加一個(gè)返回邏輯,返回值為None2024-11-11
通過(guò)python模糊匹配算法對(duì)兩個(gè)excel表格內(nèi)容歸類
這篇文章主要介紹了通過(guò)python模糊匹配算法對(duì)兩個(gè)excel表格內(nèi)容歸類,比如兩個(gè)不同的工程項(xiàng)目針對(duì)的對(duì)象都是A,那么就需要將這兩個(gè)工程項(xiàng)目歸類到A當(dāng)中,可以減少很大一部分工作量,,需要的朋友可以參考下2023-03-03
python代數(shù)式括號(hào)有效性檢驗(yàn)示例代碼
這篇文章主要給大家介紹了關(guān)于python代數(shù)式括號(hào)有效性檢驗(yàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Python中tkinter的用戶登錄管理的實(shí)現(xiàn)
這篇文章主要介紹了Python中tkinter的用戶登錄管理的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

