I am a beginner in python and I am facing an issue.
Below is my code:
class LocalQueryDictionary(dict):
def __init__(self):
self = dict()
def addvalue(self, key, value):
self[key] = value
#Global definition of the object, "local_query_dict"
local_query_dict = LocalQueryDictionary()
def save_local_query(query_name_input, query_val_input):
local_query_dict.addvalue(str(query_name_input.get()), str(query_val_input.get()))
localQuery_listbox.insert(END, str(query_name_input.get()))
I am getting "AttributeError: 'dict' object has no attribute 'addvalue'".
Please help me on the same. Thanks in advance!!
If you are subclassing dict for a reason, just initialize it.
And for adding values you can try __setitem__
class LocalQueryDictionary(dict):
def __init__(self):
super().__init__()
def addvalue(self, key, value):
self.__setitem__(key, value)
Then
a = LocalQueryDictionary()
a.addvalue('product', 'Lorem')
a.addvalue('state', 'NY')
print(a)
#=> {'product': 'Lorem', 'state': 'NY'}
The conventional name 'self' is a reference to the class.
If I understand your intention correctly, then what you probably want is this:-
class LocalQueryDictionary(dict):
def __init__(self):
pass
def addvalue(self, key, value):
self[key] = value
lqd = LocalQueryDictionary()
lqd.addvalue('k', 'v')
print(lqd)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.