开发者

append list within classes (python)

I've run into a problem with inheritance in python that I know how to avoid, but don't completely understand. The problem occured while making a menu but I've stripped down the code to only the real problem. code:

class menu:
    buttonlist=[]

>>> class av(menu):
    def __init__(self, num):
        self.button开发者_运维技巧list.append(num)
        print self.buttonlist

>>> AV=av(12)
[12]
>>> class main(menu):
    def __init__(self, num):
        self.buttonlist.append(num)
        print self.buttonlist

>>> Main=main(14)
[12, 14]
>>> AV.buttonlist
[12, 14]

I'd expect to get [14] in return by the 'Main=main(14)', and [12] with the 'AV.buttonlist' but instead it seems append has appended the list in all classes and objects :S can anyone explain to me why this is?

thanks in advance!


Because buttonlist is a class variable, not an instance variable. You need to assign it in the constructor if you want it to be local to the instance.

class menu:
    def __init__(self):
        self.buttonlist = []

And then, of course, remember to call the base constructor in derived classes.


@Cat beat me to it, but here is some working code

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

class AV(Menu):
    def __init__(self, num):
        Menu.__init__(self)
        self.buttonlist.append(num)
        print self.buttonlist

class Main(Menu):
    def __init__(self, num):
        Menu.__init__(self)
        self.buttonlist.append(num)
        print self.buttonlist

>>> av = AV(12)
>>> main = Main(14)

Note that the convention is to name python classes with CamelCase so your av class would be AV and menu would be Menu. This is by no means required though.


buttonlist is a class attribute and is also mutable(lists)

since buttonlist was not defined in the __ init__ in the base class, it's a class variable and not instance variable, thus all changes from all inheritances will affect it. Meaning that it doesn't belong to any class inheriting from it but to all instances. The first instance appended 12 and the second, 14

#Main and AV.buttonlist are actually different, 
print Main
print AV
#you should get something similar to this pointing to different locations
<__main__.main object at 0x02B753F>
<__main__.av object at 0x02B6B910>

#obviouly different objects but inheriting from the same class. Thus,the class variable changes, keeps appending
class menu:
    buttonlist=""

class av(menu):
    def __init__(self, num):
        self.buttonlist = num


class main(menu):
    def __init__(self, num):
        self.buttonlist = num

a=av("food")
b=main("menu")

print(menu.buttonlist)
print(a.buttonlist)
print(b.buttonlist)

##won't work for strings as they are immutable and can't be changed after delared in the base class 

 


class av:
    def __init__(self, num):
        self.num = num

lst = []
lst.append(av(14))
lst.append(av(12))
print(str(lst[0]) + str(lst[1]))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜