python單例模式實(shí)例解析
本文實(shí)例為大家分享了python單例模式的具體代碼,供大家參考,具體內(nèi)容如下
多次實(shí)例化的結(jié)果指向同一個(gè)實(shí)例
單例模式實(shí)現(xiàn)方式
方式一:
import settings
class MySQL:
__instance = None
def __init__(self, ip, port):
self.ip = ip
self.port = port
@classmethod
def from_conf(cls):
if cls.__instance is None:
cls.__instance = cls(settings.IP,settings.PORT)
return cls.__instance
obj1 = MySQL.from_conf()
obj2 = MySQL.from_conf()
obj3 = MySQL.from_conf()
print(obj1)
print(obj2)
print(obj3)
方式二:
import settings
def singleton(cls):
_instance = cls(settings.IP, settings.PORT)
def wrapper(*args, **kwargs):
if args or kwargs:
obj = cls(*args, **kwargs)
return obj
return _instance
return wrapper
@singleton
class MySQL:
def __init__(self, ip, port):
self.ip = ip
self.port = port
obj1 = MySQL()
obj2 = MySQL()
obj3 = MySQL()
print(obj1)
print(obj2)
print(obj3)
方式三:
import settings
class Mymeta(type):
def __init__(self, class_name, class_bases, class_dic):
self.__instance = self(settings.IP, settings.PORT)
def __call__(self, *args, **kwargs):
if args or kwargs:
obj = self.__new__(self)
self.__init__(obj, *args, **kwargs)
return obj
else:
return self.__instance
class MySQL(metaclass=Mymeta):
def __init__(self, ip, port):
self.ip = ip
self.port = port
obj1 = MySQL()
obj2 = MySQL()
obj3 = MySQL()
print(obj1)
print(obj2)
print(obj3)
方式四:
def f1():
from singleton import instance
print(instance)
def f2():
from singleton import instance,MySQL
print(instance)
obj = MySQL('1.1.1.1', '3389')
print(obj)
f1()
f2()
singleton.py文件里內(nèi)容:
import settings
class MySQL:
print('run...')
def __init__(self, ip, port):
self.ip = ip
self.port = port
instance = MySQL(settings.IP, settings.PORT)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解python實(shí)現(xiàn)小波變換的一個(gè)簡(jiǎn)單例子
- Python中實(shí)現(xiàn)單例模式的n種方式和原理
- 詳解python實(shí)現(xiàn)線(xiàn)程安全的單例模式
- Python中單例模式總結(jié)
- Python中實(shí)現(xiàn)遠(yuǎn)程調(diào)用(RPC、RMI)簡(jiǎn)單例子
- Python操作json數(shù)據(jù)的一個(gè)簡(jiǎn)單例子
- 5種Python單例模式的實(shí)現(xiàn)方式
- 常見(jiàn)的在Python中實(shí)現(xiàn)單例模式的三種方法
- python單例設(shè)計(jì)模式實(shí)現(xiàn)解析
相關(guān)文章
Python 實(shí)現(xiàn)加密過(guò)的PDF文件轉(zhuǎn)WORD格式
這篇文章主要介紹了Python 實(shí)現(xiàn)加密過(guò)的PDF文件轉(zhuǎn)WORD格式,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
用Python解決計(jì)數(shù)原理問(wèn)題的方法
計(jì)數(shù)原理是數(shù)學(xué)中的重要研究對(duì)象之一,分類(lèi)加法計(jì)數(shù)原理、分步乘法計(jì)數(shù)原理是解決計(jì)數(shù)問(wèn)題的最基本、最重要的方法,也稱(chēng)為基本計(jì)數(shù)原理,它們?yōu)榻鉀Q很多實(shí)際問(wèn)題提供了思想和工具。本文教大家怎么用Python解決在數(shù)學(xué)中遇到的計(jì)數(shù)原理問(wèn)題。2016-08-08
Pandas庫(kù)中iloc[]函數(shù)的使用方法
在數(shù)據(jù)分析過(guò)程中,很多時(shí)候需要從數(shù)據(jù)表中提取出相應(yīng)的數(shù)據(jù),而這么做的前提是需要先“索引”出這一部分?jǐn)?shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Pandas庫(kù)中iloc[]函數(shù)的使用方法,需要的朋友可以參考下2023-01-01
python通用讀取vcf文件的類(lèi)(復(fù)制粘貼即可用)
這篇文章主要介紹了python通用讀取vcf文件的類(lèi)(可以直接復(fù)制粘貼使用) ,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
python使用numpy實(shí)現(xiàn)直方圖反向投影示例
今天小編就為大家分享一篇python使用numpy實(shí)現(xiàn)直方圖反向投影示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
Python PyAutoGUI實(shí)現(xiàn)自動(dòng)化任務(wù)應(yīng)用場(chǎng)景示例
這篇文章主要為大家介紹了Python PyAutoGUI實(shí)現(xiàn)自動(dòng)化任務(wù)應(yīng)用場(chǎng)景示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
基于python制作簡(jiǎn)易版學(xué)生信息管理系統(tǒng)
這篇文章主要介紹了基于python制作簡(jiǎn)易版學(xué)生信息管理系統(tǒng),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下2021-04-04

