jquery教程

推荐列表 站点导航

当前位置:首页 > jquery > jquery教程 >

python如何实现单例模式

来源:网络整理  作者:wy  发布时间:2020-12-24 12:46
jquery中文网为您提供python如何实现单例模式等资源,欢迎您收藏本站,我们将为您提供最新的python如何实现单例模式...

python如何实现单例模式

python如何实现单例模式?下面给大家带来七种不同的方法:

一:staticmethod

代码如下:

class Singleton(object): instance = None def __init__(self): raise SyntaxError('can not instance, please use get_instance') def get_instance(): if Singleton.instance is None: Singleton.instance = object.__new__(Singleton) return Singleton.instance a = Singleton.get_instance() b = Singleton.get_instance() print('a id=', id(a)) print('b id=', id(b))

该方法的要点是在__init__抛出异常,禁止通过类来实例化,只能通过静态get_instance函数来获取实例;因为不能通过类来实例化,所以静态get_instance函数中可以通过父类object.__new__来实例化。

二:classmethod

和方法一类似,代码:

class Singleton(object): instance = None def __init__(self): raise SyntaxError('can not instance, please use get_instance') def get_instance(cls): if Singleton.instance is None: Singleton.instance = object.__new__(Singleton) return Singleton.instance a = Singleton.get_instance() b = Singleton.get_instance() print('a id=', id(a)) print('b id=', id(b))

该方法的要点是在__init__抛出异常,禁止通过类来实例化,只能通过静态get_instance函数来获取实例;因为不能通过类来实例化,所以静态get_instance函数中可以通过父类object.__new__来实例化。

三:类属性方法

和方法一类似, 代码:

class Singleton(object): instance = None def __init__(self): raise SyntaxError('can not instance, please use get_instance') def get_instance(): if Singleton.instance is None: Singleton.instance = object.__new__(Singleton) return Singleton.instance a = Singleton.get_instance() b = Singleton.get_instance() print(id(a)) print(id(b))

该方法的要点是在__init__抛出异常,禁止通过类来实例化,只能通过静态get_instance函数来获取实例;因为不能通过类来实例化,所以静态get_instance函数中可以通过父类object.__new__来实例化。

四:__new__

常见的方法, 代码如下:

class Singleton(object): instance = None def __new__(cls, IT之家args, IT之家IT之家kw): if not cls.instance: # cls.instance = object.__new__(cls, IT之家args) cls.instance = super(Singleton, cls).__new__(cls, IT之家args, IT之家IT之家kw) return cls.instance a = Singleton() b = Singleton() print(id(a)) print(id(b))

相关推荐:《Python视频教程》

五:装饰器

代码如下:

def Singleton(cls): instances = {} def getinstance(): if cls not in instances: instances[cls] = cls() return instances[cls] return getinstance class MyClass: pass a = MyClass() b = MyClass() c = MyClass() print(id(a)) print(id(b)) print(id(c))

六:元类

python2版:

class Singleton(type): def __init__(cls, name, bases, dct): super(Singleton, cls).__init__(name, bases, dct) cls.instance = None def __call__(cls, IT之家args): if cls.instance is None: cls.instance = super(Singleton, cls).__call__(IT之家args) return cls.instance class MyClass(object): __metaclass__ = Singleton a = MyClass() b = MyClass() c = MyClass() print(id(a)) print(id(b)) print(id(c)) print(a is b) print(a is c)

或者:

class Singleton(type): def __new__(cls, name, bases, attrs): attrs["_instance"] = None return super(Singleton, cls).__new__(cls, name, bases, attrs) def __call__(cls, IT之家args, IT之家IT之家kwargs): if cls._instance is None: cls._instance = super(Singleton, cls).__call__(IT之家args, IT之家IT之家kwargs) return cls._instance class Foo(object): __metaclass__ = Singleton x = Foo() y = Foo() print(id(x)) print(id(y))

python3版:

class Singleton(type): def __new__(cls, name, bases, attrs): attrs['instance'] = None return super(Singleton, cls).__new__(cls, name, bases, attrs) def __call__(cls, IT之家args, IT之家IT之家kwargs): if cls.instance is None: cls.instance = super(Singleton, cls).__call__(IT之家args, IT之家IT之家kwargs) return cls.instance class Foo(metaclass=Singleton): pass x = Foo() y = Foo() print(id(x)) print(id(y))

七:名字覆盖

代码如下:

class Singleton(object): def foo(self): print('foo') def __call__(self): return self Singleton = Singleton() Singleton.foo() a = Singleton() b = Singleton() print(id(a)) print(id(b))

以上就是python如何实现单例模式的详细内容,更多请关注jquery中文网其它相关文章!

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jq/jc/8526.shtml

相关文章
最新文章
PHP识别相片是否是颠倒的 PHP识别相片是否是颠倒的

时间:2020-12-28

python编程有哪些ide python编程有哪些ide

时间:2020-12-28

python开发工程师是做什么 python开发工程师是做什么

时间:2020-12-28

php构造函数的作用 php构造函数的作用

时间:2020-12-28

php怎么跟数据库连接 php怎么跟数据库连接

时间:2020-12-28

php实现顺序线性表 php实现顺序线性表

时间:2020-12-28

Python多重继承中的菱形继 Python多重继承中的菱形继

时间:2020-12-28

php中break的作用 php中break的作用

时间:2020-12-28

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

python如何实现单例模式

2020-12-24 编辑:wy

python如何实现单例模式

python如何实现单例模式?下面给大家带来七种不同的方法:

一:staticmethod

代码如下:

class Singleton(object): instance = None def __init__(self): raise SyntaxError('can not instance, please use get_instance') def get_instance(): if Singleton.instance is None: Singleton.instance = object.__new__(Singleton) return Singleton.instance a = Singleton.get_instance() b = Singleton.get_instance() print('a id=', id(a)) print('b id=', id(b))

该方法的要点是在__init__抛出异常,禁止通过类来实例化,只能通过静态get_instance函数来获取实例;因为不能通过类来实例化,所以静态get_instance函数中可以通过父类object.__new__来实例化。

二:classmethod

和方法一类似,代码:

class Singleton(object): instance = None def __init__(self): raise SyntaxError('can not instance, please use get_instance') def get_instance(cls): if Singleton.instance is None: Singleton.instance = object.__new__(Singleton) return Singleton.instance a = Singleton.get_instance() b = Singleton.get_instance() print('a id=', id(a)) print('b id=', id(b))

该方法的要点是在__init__抛出异常,禁止通过类来实例化,只能通过静态get_instance函数来获取实例;因为不能通过类来实例化,所以静态get_instance函数中可以通过父类object.__new__来实例化。

三:类属性方法

和方法一类似, 代码:

class Singleton(object): instance = None def __init__(self): raise SyntaxError('can not instance, please use get_instance') def get_instance(): if Singleton.instance is None: Singleton.instance = object.__new__(Singleton) return Singleton.instance a = Singleton.get_instance() b = Singleton.get_instance() print(id(a)) print(id(b))

该方法的要点是在__init__抛出异常,禁止通过类来实例化,只能通过静态get_instance函数来获取实例;因为不能通过类来实例化,所以静态get_instance函数中可以通过父类object.__new__来实例化。

四:__new__

常见的方法, 代码如下:

class Singleton(object): instance = None def __new__(cls, IT之家args, IT之家IT之家kw): if not cls.instance: # cls.instance = object.__new__(cls, IT之家args) cls.instance = super(Singleton, cls).__new__(cls, IT之家args, IT之家IT之家kw) return cls.instance a = Singleton() b = Singleton() print(id(a)) print(id(b))

相关推荐:《Python视频教程》

五:装饰器

代码如下:

def Singleton(cls): instances = {} def getinstance(): if cls not in instances: instances[cls] = cls() return instances[cls] return getinstance class MyClass: pass a = MyClass() b = MyClass() c = MyClass() print(id(a)) print(id(b)) print(id(c))

六:元类

python2版:

class Singleton(type): def __init__(cls, name, bases, dct): super(Singleton, cls).__init__(name, bases, dct) cls.instance = None def __call__(cls, IT之家args): if cls.instance is None: cls.instance = super(Singleton, cls).__call__(IT之家args) return cls.instance class MyClass(object): __metaclass__ = Singleton a = MyClass() b = MyClass() c = MyClass() print(id(a)) print(id(b)) print(id(c)) print(a is b) print(a is c)

或者:

class Singleton(type): def __new__(cls, name, bases, attrs): attrs["_instance"] = None return super(Singleton, cls).__new__(cls, name, bases, attrs) def __call__(cls, IT之家args, IT之家IT之家kwargs): if cls._instance is None: cls._instance = super(Singleton, cls).__call__(IT之家args, IT之家IT之家kwargs) return cls._instance class Foo(object): __metaclass__ = Singleton x = Foo() y = Foo() print(id(x)) print(id(y))

python3版:

class Singleton(type): def __new__(cls, name, bases, attrs): attrs['instance'] = None return super(Singleton, cls).__new__(cls, name, bases, attrs) def __call__(cls, IT之家args, IT之家IT之家kwargs): if cls.instance is None: cls.instance = super(Singleton, cls).__call__(IT之家args, IT之家IT之家kwargs) return cls.instance class Foo(metaclass=Singleton): pass x = Foo() y = Foo() print(id(x)) print(id(y))

七:名字覆盖

代码如下:

class Singleton(object): def foo(self): print('foo') def __call__(self): return self Singleton = Singleton() Singleton.foo() a = Singleton() b = Singleton() print(id(a)) print(id(b))

以上就是python如何实现单例模式的详细内容,更多请关注jquery中文网其它相关文章!

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jq/jc/8526.shtml

相关文章

风云图片

推荐阅读

返回jquery教程频道首页