开发者

Python: Problems adding items to list in a class

开发者 https://www.devze.com 2023-01-25 05:39 出处:网络
I\'ve got a class defined with a method to add items to it: class ProdReg: 开发者_如何学运维def __init__(self):

I've got a class defined with a method to add items to it:

class ProdReg:
    开发者_如何学运维def __init__(self):
        self.__PListe=[]
    def addProdukt(self,pItem): 
        self.__Pliste.append(pItem)

When I instantiate a ProdReg object and try to add an object to it with the following code i gent an error:

pr.addProdukt(b)

I get the following error: AttributeError: 'ProdReg' object has no attribute '_ProdReg__Pliste'

What's wrong? I'm not able to figure thisone out.

/Andy.l


Because in the __init__ you wrote: __PListe and in the the addProdukt method, you wrote __Pliste. Python is case sensitive.


It's a typo in your code I think, or a misunderstand of how names work. In Python names are case-sensitive.

You add the attribute as PListe then reference it as Pliste. In one in the L is lower case and in the other it is upper case.

0

精彩评论

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