如何保留现有实例的集合并返回一个oninstantiation [英] how to keep collection of existing instances and return one oninstantiation
问题描述
我想不出一个好主题..
基本上,我说有一个班级
班级垃圾邮件:
def __init __(self,x):
self.x = x
然后如果我创建两个实例:
a =垃圾邮件(''foo'')
b =垃圾邮件(''foo'')
a == b#False
我真正想要的是保留所有垃圾邮件实例的集合,
以及我是否尝试使用相同的构造函数创建新的垃圾邮件实例
参数,然后返回现有的Spam实例。我认为新式的
课程会这样做:
类垃圾邮件(对象):
cache = {}
def __new __(cls,x):
如果cls.cache.has_key(x):
返回cls.cache [x]
def __init __(self,x):
self.x = x
self.cache [x] = self
a =垃圾邮件(''foo'')
b =垃圾邮件(''foo'')
嗯,在这种情况下a和b是完全相同的......没有!我假设这是
,因为__new__中的测试失败所以它返回None,我需要然后
创建一个新的Spam ..但是如何在不调用__new__的情况下这样做再次?
我不能打电话给__init__因为没有自我...
那么最好/首选的方式是什么? ?
I couldn''t think of a good subject..
Basically, say I have a class
class Spam:
def __init__(self, x):
self.x = x
then if I create two instances:
a = Spam(''foo'')
b = Spam(''foo'')
a == b # False
What I *really* want is to keep a collection of all the Spam instances,
and if i try to create a new Spam instance with the same contructor
parameters, then return the existing Spam instance. I thought new-style
classes would do it:
class Spam(object):
cache = {}
def __new__(cls, x):
if cls.cache.has_key(x):
return cls.cache[x]
def __init__(self, x):
self.x = x
self.cache[x] = self
a = Spam(''foo'')
b = Spam(''foo'')
Well, in this case a and b are identical... to None! I assume this is
because the test in __new__ fails so it returns None, I need to then
create a new Spam.. but how do I do that without calling __new__ again?
I can''t call __init__ because there''s no self...
So what is the best/preferred way to do this?
推荐答案
我真正想要的是保留所有垃圾邮件实例的集合,
如果我尝试使用相同的构造函数
参数创建一个新的Spam实例,则返回现有的Spam实例。我认为新式的课程会这样做:
< snip>
那么最好/首选的方法是什么?
What I *really* want is to keep a collection of all the Spam instances,
and if i try to create a new Spam instance with the same contructor
parameters, then return the existing Spam instance. I thought new-style
classes would do it: <snip>
So what is the best/preferred way to do this?
使用BORG模式。请参阅
http://aspn.activestate.com /ASPN/Coo...n/Recipe/66531
加上你的缓存,这应该可以解决问题。
Diez
Use the BORG-pattern. See
http://aspn.activestate.com/ASPN/Coo...n/Recipe/66531
Together with your caching, that should do the trick.
Diez
> class Spam(object):
> class Spam(object):
cache = {}
def __new __(cls,x):
如果cls.cache.has_key(x):
返回cls .cache [x]
def __init __(self,x):
self.x = x
self.cache [x] = self
a =垃圾邮件( ''foo'')
b =垃圾邮件(''foo'')
嗯,在这种情况下,a和b是相同的......没有!我认为这是
因为__new__中的测试失败所以它返回None,我需要然后创建一个新的Spam ..但是如何在不再调用__new__
的情况下这样做呢?我不能叫__init__因为没有自我......
cache = {}
def __new__(cls, x):
if cls.cache.has_key(x):
return cls.cache[x]
def __init__(self, x):
self.x = x
self.cache[x] = self
a = Spam(''foo'')
b = Spam(''foo'')
Well, in this case a and b are identical... to None! I assume this is
because the test in __new__ fails so it returns None, I need to then
create a new Spam.. but how do I do that without calling __new__
again?
I can''t call __init__ because there''s no self...
哎呀,你忘了归还对象.__ new __(cls ,x)如果
对象不在缓存中。这应该解决它。
Jonathan
http:// cleverdevil.org
marduk写道:
marduk wrote:
我真正想要的是保留所有的集合垃圾邮件实例,如果我尝试使用相同的构造函数
参数创建新的垃圾邮件实例,则返回现有的垃圾邮件实例。我认为新式的课程会这样做:
类垃圾邮件(对象):
缓存= {}
def __new __(cls,x):
如果cls.cache.has_key(x):
返回cls.cache [x]
缓存未命中时,隐式返回None。但是如果__new __()返回一个Spam实例,你的__init __()方法只会被调用。
def __init __(self,x):
self.x = x
self.cache [x] = self
a =垃圾邮件(''foo'')
b =垃圾邮件(''foo'')
那么,在这种情况下,a和b是相同的......到无!我认为这是
因为__new__中的测试失败所以它返回None,我需要创建一个新的Spam ..但是如何在不再调用__new__的情况下这样做呢?
我可以不要叫__init__因为没有自我...
那么最好/首选的方法是什么?
What I *really* want is to keep a collection of all the Spam instances,
and if i try to create a new Spam instance with the same contructor
parameters, then return the existing Spam instance. I thought new-style
classes would do it:
class Spam(object):
cache = {}
def __new__(cls, x):
if cls.cache.has_key(x):
return cls.cache[x]
On cache misses you implicitly return None. But your __init__() method will
only be called if __new__() returns a Spam instance.
def __init__(self, x):
self.x = x
self.cache[x] = self
a = Spam(''foo'')
b = Spam(''foo'')
Well, in this case a and b are identical... to None! I assume this is
because the test in __new__ fails so it returns None, I need to then
create a new Spam.. but how do I do that without calling __new__ again?
I can''t call __init__ because there''s no self...
So what is the best/preferred way to do this?
class Spam(object):
cache = {}
def __new __(cls,x):
试试:
inst = cls.cache [x]
print" from cache"
除了KeyError:
cls.cache [x] = inst = object .__ new __(cls)
print" new instance"
return inst#始终返回垃圾邮件实例
def __init __(self,x):
#将一次性初始化放入__new __(),因为__init __()
#也会被缓存命中的实例调用。
print" init",x
a =垃圾邮件(''foo'')
b =垃圾邮件('' foo'')
打印a,b,a是b
彼得
class Spam(object):
cache = {}
def __new__(cls, x):
try:
inst = cls.cache[x]
print "from cache"
except KeyError:
cls.cache[x] = inst = object.__new__(cls)
print "new instance"
return inst # always return a Spam instance
def __init__(self, x):
# put one-off initialization into __new__() because __init__()
# will be called with instances from cache hits, too.
print "init", x
a = Spam(''foo'')
b = Spam(''foo'')
print a, b, a is b
Peter
这篇关于如何保留现有实例的集合并返回一个oninstantiation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!