#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Smile Error I got when i use dictionary in a python class


    Hi This is the my class and i usesd 2 dictionary structure in the class .
    [B]class SNMPCsvGen:


    dict = {}
    dict2 = {}
    def __init__(self):
    #dict = {} #This is dictionary of key value pair of SNMP param name and its value
    #dict2 = {} #This is dictionary of standard key value pair of SNMP param name and its value
    print " hello "

    def key_value_gen(self,file_name): # Here file name is , csv file contais pairs of SNMP parameter name and its value
    keys = []
    values = []
    f = csv.reader(open(file_name,"r"))
    count = 0
    for row in f:
    keys.append(row[0])
    values.append(row[1])
    dict[keys[count]] = values[count]
    count = count + 1


    def main():
    snmp_csv_obj = SNMPCsvGen() # Constructor of the class creates object
    snmp_csv_obj.key_value_gen("comm_file") #csv file created,having snmpparm value

    When i run one function of the class, i am getting following error.

    dict[keys[count]] = values[count]
    TypeError: 'type' object does not support item assignment


    So my question is how can i remove this error, by using dictionary as a member of the class.
    Thanks in advance for your help
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by RajatShukla
    [...]

    dict[keys[count]] = values[count]
    TypeError: 'type' object does not support item assignment


    So my question is how can i remove this error, by using dictionary as a member of the class.
    Thanks in advance for your help
    You are using a reserved word (dict) as variable name, change it to something else.

    Also, use CODE tags to make it easier to read your code. Please also post the entire code, your example is missing some imports.

    What version of python are you using?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by partoj
    You are using a reserved word (dict) as variable name, change it to something else.

    Also, use CODE tags to make it easier to read your code. Please also post the entire code, your example is missing some imports.

    What version of python are you using?

    Hi,
    I removed this error and my code is running. I changed dict into snmp_dict and wherever i access it i use following command self.snm_dict
    I am using python version 2.5.5.

    Thanks for your help.

IMN logo majestic logo threadwatch logo seochat tools logo