473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

replacing one instance with another

Hi,
I'm not sure why this doesn't work:
>>>dic_myinstances={}
>>>class MyClass(object):
def __init__(self, id):
global dic_myinstances
if dic_myinstances.has_key(id):
self = dic_myinstances[id]
else:
dic_myinstances[id] = self
>>>ins1 = MyClass('xx')
ins2 = MyClass('yy')
ins3 = MyClass('xx')
ins3 is ins1
False

Why isn't ins3 the same as ins1? How can I get this to work?

thanks

Mar 26 '07 #1
3 925
Hi,

I solved it myself! I realised that __new__ creates self prior to
init, so this works:
>>>dic_myinstances={}
>>>class MyClass(object):
def __new__(self,id):
global dic_myinstances
if dic_myinstances.has_key(id):
return dic_myinstances[id]
else:
dic_myinstances[id] = self
return self

>>>ins1 = MyClass('xx')
ins2 = MyClass('yy')
ins3 = MyClass('xx')
print ins3 is ins1
True

Is this the best way to do this?

Mar 26 '07 #2
Hi, yet again to myself!

I've realised after further testing and reading that I actually need
to do this:
>>>dic_myinstances={}
class MyClass(object):
def __new__(cls,id):
global dic_myinstances
if dic_myinstances.has_key(id):
return dic_myinstances[id]
else:
dic_myinstances[id] = super(MyClass, cls).__new__(cls, id)
return dic_myinstances[id]
def __init__(self,id):
print id
>>>ins1 = MyClass('xx')
'xx'
>>>ins2 = MyClass('yy')
'yy'
>>>ins3 = MyClass('xx')
'xx'
>>>ins3 is ins1
True

Mar 26 '07 #3
En Sun, 25 Mar 2007 23:34:51 -0300, manstey <ma*****@csu.edu.auescribió:
I've realised after further testing and reading that I actually need
to do this:
>>>dic_myinstances={}
class MyClass(object):
def __new__(cls,id):
global dic_myinstances
if dic_myinstances.has_key(id):
return dic_myinstances[id]
else:
dic_myinstances[id] = super(MyClass, cls).__new__(cls, id)
return dic_myinstances[id]
def __init__(self,id):
print id
>>>ins1 = MyClass('xx')
'xx'
>>>ins2 = MyClass('yy')
'yy'
>>>ins3 = MyClass('xx')
'xx'
>>>ins3 is ins1
True
That's fine, but notice that __init__ is called even if the instance
already exists. That's usually undesirable, and you can use a factory
function instead:

def MyClassFactory(id):
inst = dic_myinstances.get(id)
if inst is None:
dic_myinstances[id] = inst = MyClass(id)
return inst

(Notice that no global statement is needed, even on your __new__)
You can make it a static method if you wish.

--
Gabriel Genellina

Mar 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: StenKoll | last post by:
Help needed in order to create a register of stocks in a company. In accordance with local laws I need to give each individual share a number. I have accomplished this by establishing three tables...
31
by: J.S. | last post by:
Let's say I have a text file with parameters like the following embedded in the text: @@Textbox1@@, @@Textbox2@@, etc. Is it possible to replace the parameters in the text file with values...
12
by: anonymous | last post by:
Hello, I need to replace this char  with another char. However I am not able to acieve this. I tried this but it doesnt work: str = str.Replace(chr(asc(194)), "") Can somebody help ?
3
by: TobyD | last post by:
I am a beginner programmer in VB and need help. If I have a string with a return, how do I exchange the return with a return and tab? Currently I am entering a string with EnterKeyBehavior...
14
by: Adnan Siddiqi | last post by:
Hi Suppose I have following URLs comming from an HTML document <a href="http://mydomain1.com">Domain1</a> <a...
2
by: James Fifth | last post by:
Hello and God Bless, I am stumped trying to get a simple xml database replacing certain data with other data programmatically. This is what my xml looks like. ...
1
by: patelgaurav85 | last post by:
Hi, I want to convert xml in one format into another xml format shown below Input xml : <Name> <Name1> <Name11>Name11</Name11> <Name12>Name12</Name12>
10
by: Scott M. | last post by:
I've seen many posts and ready articles discussing how changing the membership & roles "provider" in VS .NET is easy, but have yet to see instructions on how to do it. If I already have SQL...
1
by: Matt Herzog | last post by:
Hey All. I'm learning some python with the seemingly simple task of updating a firewall config file with the new IP address when my dhcpd server hands one out. Yeah, I know it's dangerous to edit...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.