Jump to content
  • Advertisement
Sign in to follow this  
macktruck6666

Python bug???

This topic is 3069 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The problem is that I expect the output to be:
0
0
instead it is:
0
1

Is there a solution to the following code?

class Menu:
subMenuList = []
def addSubMenu(self, newSubMenu):
print len(newSubMenu.subMenuList)
self.subMenuList.append(newSubMenu)
print len(newSubMenu.subMenuList)
mainMenu = Menu()
subMenu = Menu()
mainMenu.addSubMenu(subMenu)


[Edited by - macktruck6666 on July 14, 2010 10:34:48 PM]

Share this post


Link to post
Share on other sites
Advertisement
append adds a new element to the list, which, in this case, is [].
So the list looks like this after the append: [[]]. That's a list containing one element; the one element being [].
Also, you can use the [code] and tags in the forums to preserve indentation. Look here: GDNet Forums FAQ

Share this post


Link to post
Share on other sites
The item that is added is actually a menu object. I've tested it.
So, mainMenu.subMenuList looks like [menu object]
and, subMenu.subMenuList looks like [menu object]
and, subMenu.subMenuList[0].subMenuList[0].subMenuList looks like [menu object]

Share this post


Link to post
Share on other sites
Riight? So after the append, newSubMenu.subMenuList contains one item. Forget the nesting hoohah -- why would you expect a list to be empty, when you just put something in it?

Share this post


Link to post
Share on other sites
I put something into Main.subMenuList not into subMenu.subMenuList
or in context of addSubMenu method
I put something into self.subMenuList not into self.subMenuList[0].subMenuList

Share this post


Link to post
Share on other sites
Quote:
Original post by macktruck6666
I put something into Main.subMenuList not into subMenu.subMenuList
or in context of addSubMenu method
I put something into self.subMenuList not into self.subMenuList[0].subMenuList

Wrong.

Share this post


Link to post
Share on other sites
Ah, I didn't understand what was going on here either.

So if I debluntize Ollies reply, the subMenuList member doesn't actually belong to either of the Menu objects; it belongs to the Menu class and is therefore shared by both objects.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
Ah, I didn't understand what was going on here either.

So if I debluntize Ollies reply, the subMenuList member doesn't actually belong to either of the Menu objects; it belongs to the Menu class and is therefore shared by both objects.


Correct!




class Menu:
def __init__(self):
self.subMenuList = []

def addSubMenu(self, newSubMenu):
print len(newSubMenu.subMenuList)
self.subMenuList.append(newSubMenu)
print len(newSubMenu.subMenuList)

mainMenu = Menu()
subMenu = Menu()
mainMenu.addSubMenu(subMenu)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!