如何创建一个不会重新创建具有相同输入参数的对象的类
我正在尝试创建一个不会重新创建具有相同输入参数的对象的类。 当我尝试使用与创建已存在对象相同的参数实例化一个类时,我只希望我的新类返回指向已创建(昂贵创建的)对象的指针。 这是我到目前为止所尝试过的:
class myobject0(object):
# At first, I didn't realize that even already-instantiated
# objects had their __init__ called again
instances = {}
def __new__(cls,x):
if x not in cls.instances.keys():
cls.instances[x] = object.__new__(cls,x)
return cls.instances[x]
def __init__(self,x):
print 'doing something expensive'
class myobject1(object):
# I tried to override the existing object's __init__
# but it didnt work.
instances = {}
def __new__(cls,x):
if x not in cls.instances.keys():
cls.instances[x] = object.__new__(cls,x)
else:
cls.instances[x].__init__ = lambda x: None
return cls.instances[x]
def __init__(self,x):
print 'doing something expensive'
class myobject2(object):
# does what I want but is ugly
instances = {}
def __new__(cls,x):
if x not in cls.instances.keys():
cls.instances[x] = object.__new__(cls,x)
cls.instances[x]._is_new = 1
else:
cls.instances[x]._is_new = 0
return cls.instances[x]
def __init__(self,x):
if self._is_new:
print 'doing something expensive'
这是我第一次尝试覆盖 __new__ ,并且我确信我没有以正确的方式处理它。 请让我正视一下。
I am trying to create a class that doesn't re-create an object with the same input parameters. When I try to instantiate a class with the same parameters that were used to create an already-existing object, I just want my new class to return a pointer to the already-created (expensively-created) object. This is what I have tried so far:
class myobject0(object):
# At first, I didn't realize that even already-instantiated
# objects had their __init__ called again
instances = {}
def __new__(cls,x):
if x not in cls.instances.keys():
cls.instances[x] = object.__new__(cls,x)
return cls.instances[x]
def __init__(self,x):
print 'doing something expensive'
class myobject1(object):
# I tried to override the existing object's __init__
# but it didnt work.
instances = {}
def __new__(cls,x):
if x not in cls.instances.keys():
cls.instances[x] = object.__new__(cls,x)
else:
cls.instances[x].__init__ = lambda x: None
return cls.instances[x]
def __init__(self,x):
print 'doing something expensive'
class myobject2(object):
# does what I want but is ugly
instances = {}
def __new__(cls,x):
if x not in cls.instances.keys():
cls.instances[x] = object.__new__(cls,x)
cls.instances[x]._is_new = 1
else:
cls.instances[x]._is_new = 0
return cls.instances[x]
def __init__(self,x):
if self._is_new:
print 'doing something expensive'
This is my first venture into overriding __new__
and I'm convinced I'm not going about it the right way. Set me straight, please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个类装饰器,用于使类成为多类:(
这是 PEP 318 中单例装饰器的一个轻微变体。)
然后,要使类成为多类,请使用装饰器:
现在,如果您使用相同的 id 实例化 MyObject,你得到相同的实例:
Here's a class decorator to make a class a multiton:
(This is a slight variant of the singleton decorator from PEP 318.)
Then, to make your class a multiton, use the decorator:
Now, if you instantiate MyObject with the same id, you get the same instance:
首先,在 Python 中使用大写的类名。
其次,使用工厂设计模式来解决这个问题。
这比使用 new 和类级别的对象池要简单得多。
First, use Upper Case Class Names in Python.
Second, use a Factory design pattern to solve this problem.
This is much simpler than fooling around with new and having class level pools of objects.
这是我对 Jerry 方法的实现,使用数组作为池
,然后使用元类的另一种方法,以便您可以继承功能。 这个使用weakreaf valuedicts作为池,因此对象可以更好地被垃圾收集
Here is my implementation of Jerry's way, using an array as the pool
and then another way using metaclasses so you can inherit the functionality. This one uses weakreaf valuedicts as the pool, so the objects get garbage collected better