473,473 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Python: Are these the same object, what's going on?

1 New Member
Right, heres the piece of code:

Expand|Select|Wrap|Line Numbers
  1. class Circuit:
  2.  
  3.     pass
  4.  
  5. class Workspace:
  6.  
  7.     Circuits = {}
  8.     Components = {}
  9.  
  10.  
  11. WORKSPACES = {}
  12.  
  13.  
  14. WORKSPACES["a"] = Workspace()
  15. WORKSPACES["b"] = Workspace()
  16. WORKSPACES["c"] = Workspace()
  17.  
  18. #WORKSPACES["a"].Circuits["r"] = Circuit()
  19.  
  20. print WORKSPACES["a"].Circuits
  21. print WORKSPACES["b"].Circuits
  22. print WORKSPACES["c"].Circuits
  23.  
Now, the output from this is:

{'r': <__main__.Circuit instance at 0x04AD2E90>}
{'r': <__main__.Circuit instance at 0x04AD2E90>}
{'r': <__main__.Circuit instance at 0x04AD2E90>}

but this isn't what I'm expecting, or what I want. TO put it best, I want the behavoius that matches this output:

{'r': <__main__.Circuit instance at 0x04AD2E90>}
{}
{}


Can anyone explain to me what's goig on here, and how I can get the behaviour I want?
May 20 '08 #1
1 1147
jlm699
314 Contributor
What's going on is your class is not correct. You need to initialize your class with an __init__ function and use the self reference; otherwise all instances of this class will reference the same variables.
Expand|Select|Wrap|Line Numbers
  1. >>> class Workspace2:
  2. ...     def __init__(self):
  3. ...         self.Circuits = {}
  4. ...         self.Components = {}
  5. ...     
  6. >>> Workspaces2['a'] = Workspace2()
  7. >>> Workspaces2['b'] = Workspace2()
  8. >>> Workspaces2['c'] = Workspace2()
  9. >>> Workspaces2['a'].Circuits['r'] = Circuit()
  10. >>> print Workspaces2['a'].Circuits
  11. {'r': <__main__.Circuit instance at 0x01C894E0>}
  12. >>> print Workspaces2['b'].Circuits
  13. {}
  14. >>> print Workspaces2['c'].Circuits
  15. {}
  16. >>> 
  17.  
May 20 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: dmbkiwi | last post by:
I am new to this group, and relatively new to python programming, however, have encountered a problem I just cannot solve through reading the documentation, and searching this group on google. I...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
14
by: David MacQuigg | last post by:
I am starting a new thread so we can avoid some of the non-productive argument following my earlier post "What is good about Prothon". At Mr. Hahn's request, I will avoid using the name "Prothon"...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.