Jjang
Jjang

Reputation: 11444

python 3 self class dict

I am trying to create my own class of dictionary in python 3 which has a field of dict variable and setitem and getitem methods. Though, it doesnt work for some reason. Tried to look around but couldn't find the answer.

class myDictionary:
    def __init(self):
        self.myDic={}

    def __setitem__(self, key, value):
        self.myDic[key]=value

I'm getting:

'myDictionary' object has no attribute 'myDic'

Any ideas? :)

Upvotes: 0

Views: 305

Answers (2)

user278064
user278064

Reputation: 10180

You've a typo within your __init__ method. Change it:

class myDictionary:
   def __init__(self):
      self.myDic={}

   def __setitem__(self, key, value):
      self.myDic[key]=valu

Upvotes: 1

Andrew Clark
Andrew Clark

Reputation: 208655

You forgot the trailing underscores on __init__(), try the following:

class myDictionary:
    def __init__(self):
        self.myDic={}

    def __setitem__(self, key, value):
        self.myDic[key]=value

Upvotes: 2

Related Questions