This little program just doesn't do what I think it should and I cannot understand what the problem is.
I'm running Python 2.4 on Windoze XP.
It contains an album class, an album collection class, a track class, and a track collection class.
It seems confused about which instance is which -- when it says Albums.Tracks.Count() the two instances are unique, but when it says Album.Tracks you get both instances combined. It's as if all accessors in Python are equal, but some accessors are more equal than others :-)
Here's the code. It is the smallest example I could make to demonstrate this behavior. -
# Making a nested set of objects
-
# A track
-
class TrackClass:
-
def __init__(self, ID, Title):
-
self.ID = ID
-
self.Title = Title
-
-
# A collection of Tracks
-
class TracksClass:
-
Items = []
-
ItemCount = 0
-
def Add(self, ID, Title):
-
self.Items.append(TrackClass(ID,Title))
-
self.ItemCount += 1
-
def Count(self): return self.ItemCount
-
def __getitem__(self,ordinal): return self.Items[ordinal]
-
-
class AlbumClass:
-
def __init__(self,ID,Title):
-
self.ID = ID
-
self.Title = Title
-
self.Tracks = TracksClass()
-
# A collection of Albums
-
class AlbumsClass:
-
Items = []
-
ItemCount = 0
-
def Add(self,ID,Title):
-
self.Items.append(AlbumClass(ID,Title))
-
self.ItemCount += 1
-
def __getitem__(self,ordinal): return self.Items[ordinal]
-
def Count(self) : return self.ItemCount
-
-
-
Albums = AlbumsClass()
-
Albums.Add(ID = 1, Title = "First Album")
-
Albums[0].Tracks.Add(ID = 1, Title = "First Album First Track")
-
Albums[0].Tracks.Add(ID = 2, Title = "First Album Second Track")
-
Albums.Add(ID = 2, Title = "Second Album")
-
Albums[1].Tracks.Add(ID = 3, Title = "Second Album First Track")
-
Albums[1].Tracks.Add(ID = 4, Title = "Second Album Second Track")
-
Albums[1].Tracks.Add(ID = 5, Title = "Second Album Third Track")
-
-
print "This does not print the expected results"
-
for Album in Albums:
-
print "Album (%i) %s has %i tracks" % (Album.ID, Album.Title, Album.Tracks.Count())
-
for Track in Album.Tracks:
-
print " Track: (%i) %s" % (Track.ID, Track.Title)
-
print ""
6 2699 bvdet 2,851
Expert Mod 2GB
This little program just doesn't do what I think it should and I cannot understand what the problem is.
I'm running Python 2.4 on Windoze XP.
It contains an album class, an album collection class, a track class, and a track collection class.
It seems confused about which instance is which -- when it says Albums.Tracks.Count() the two instances are unique, but when it says Album.Tracks you get both instances combined. It's as if all accessors in Python are equal, but some accessors are more equal than others :-)
Here's the code. It is the smallest example I could make to demonstrate this behavior.
-
# Making a nested set of objects
-
# A track
-
class TrackClass:
-
def __init__(self, ID, Title):
-
self.ID = ID
-
self.Title = Title
-
-
# A collection of Tracks
-
class TracksClass:
-
Items = []
-
ItemCount = 0
-
def Add(self, ID, Title):
-
self.Items.append(TrackClass(ID,Title))
-
self.ItemCount += 1
-
def Count(self): return self.ItemCount
-
def __getitem__(self,ordinal): return self.Items[ordinal]
-
-
class AlbumClass:
-
def __init__(self,ID,Title):
-
self.ID = ID
-
self.Title = Title
-
self.Tracks = TracksClass()
-
# A collection of Albums
-
class AlbumsClass:
-
Items = []
-
ItemCount = 0
-
def Add(self,ID,Title):
-
self.Items.append(AlbumClass(ID,Title))
-
self.ItemCount += 1
-
def __getitem__(self,ordinal): return self.Items[ordinal]
-
def Count(self) : return self.ItemCount
-
-
-
Albums = AlbumsClass()
-
Albums.Add(ID = 1, Title = "First Album")
-
Albums[0].Tracks.Add(ID = 1, Title = "First Album First Track")
-
Albums[0].Tracks.Add(ID = 2, Title = "First Album Second Track")
-
Albums.Add(ID = 2, Title = "Second Album")
-
Albums[1].Tracks.Add(ID = 3, Title = "Second Album First Track")
-
Albums[1].Tracks.Add(ID = 4, Title = "Second Album Second Track")
-
Albums[1].Tracks.Add(ID = 5, Title = "Second Album Third Track")
-
-
print "This does not print the expected results"
-
for Album in Albums:
-
print "Album (%i) %s has %i tracks" % (Album.ID, Album.Title, Album.Tracks.Count())
-
for Track in Album.Tracks:
-
print " Track: (%i) %s" % (Track.ID, Track.Title)
-
print ""
I added code tags for you. Now we can see your code structure.
Well, this looks like a BUG to me -- if someone with a newer version (I'm on 2.4) would run this and let me know what the results are I'd appreciate it.
The MooseMiester
bvdet 2,851
Expert Mod 2GB
Well, this looks like a BUG to me -- if someone with a newer version (I'm on 2.4) would run this and let me know what the results are I'd appreciate it.
The MooseMiester
I see no bug. You need an __init__ method in TracksClass to create an instance. The tracks are accumulating as a class attribute.
Here's the deal. When you declare class variables as in: -
# A collection of Tracks
-
class TracksClass:
-
Items = []
-
ItemCount = 0
you create variables shared by all instances.
Instance variables are always assigned as
Preferably, but not necessarily, in __init__().
Thanks bartonc
I would not have guessed that it worked this way in a hundred years, as I expect something called a "class" to exhibit a degree of "encapsulation" without me having to explicitly make that happen -- Silly me!!
What IDE are you using? Coming from C++ C# what's missing the most in Python is a good IDE, like Visual Studio from MickeySoft.
Thanks Again
The MooseMiester
Thanks bartonc
I would not have guessed that it worked this way in a hundred years, as I expect something called a "class" to exhibit a degree of "encapsulation" without me having to explicitly make that happen -- Silly me!!
I have to disagree with you, here. This is an ideal implementation of the object model: Just as two (or more) objects in the real world may share resources, so, too, may instances of Python objects. Consider two debit cards on an account : Each has its own PIN (instance variable), but using one to withdraw money will effect the balance available to the other.
What IDE are you using? Coming from C++ C# what's missing the most in Python is a good IDE, like Visual Studio from MickeySoft.
Thanks Again
The MooseMiester
I use Boa Constructor. I find it flexible and stable. And it's free. For other options, check out this review.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: David MacQuigg |
last post by:
I am writing a chapter for teaching OOP in Python. This chapter is
intended as a brief introduction to replace the more complete
discussion in Learning Python, 2nd ed, pp. 295-390. I need to...
|
by: Kenneth McDonald |
last post by:
I've recently used subclasses of the builtin str class to good effect.
However, I've been unable to do the following:
1) Call the constructor with a number of arguments other than
the number of...
|
by: Mon |
last post by:
I am in the process of reorganizing my code and came across and I came
across a problem, as described in the subject line of this posting.
I have many classes that have instances of other classes...
|
by: JackRazz |
last post by:
Is it possible to serialize a static class containing shared variables like I can
serialize an instance of a normal class (obj)?
I had a static class, but I gave up and converted it into a normal...
|
by: Jordan Marr |
last post by:
I have the following class:
class ProvisionCollection
{
...
private int m_VarianceCount;
public int VarianceCount
{
get { return m_VarianceCount; }
|
by: JonathanB |
last post by:
Ok, I know there has to be a way to do this, but my google-fu fails me
(once more). I have a class with instance variables (or should they be
called attributes, I'm newish to programming and get...
|
by: inadsad |
last post by:
Good Day Group,
Recently I started a small test project in C# VS 05 for learning
purposes. I have a class and a form. I defined a property in a class
with get/set and create an instance of...
|
by: Nikolaus Rath |
last post by:
Hello,
I am really surprised that I am asking this question on the mailing
list, but I really couldn't find it on python.org/doc.
Why is there no proper way to protect an instance variable...
|
by: Joe Strout |
last post by:
On Nov 10, 2008, at 2:44 PM, Zane Selvans wrote:
How are you creating your list? You need to make sure that each
instance creates its very own list object, something like:
self.foo =
...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |