1,105,380 Community Members

Dictionaries and class variables

Member Avatar
sebcbien
Newbie Poster
10 posts since Jul 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello,

I'm trying to update a dictionary using variable in a class. Something looking like this :

class testClass(object): 
    def __init__(self):
        self.data0 = 0
        self.data1 = 1
        self.data2 = 2
        self.data3 = 3
        self.d = dict( data0 = self.data0, data1 = self.data1, data2 = self.data2, data3 = self.data3 )
    def updateData(self,data):
        self.data0 = data[0]
        self.data1 = data[1]
        self.data2 = data[2]
        self.data3 = data[3]
        for key in self.d:
            print key + ' : ' + str(self.d[key])
            

if __name__ == '__main__':
    test = testClass()
    data = [4,5,6,7]
    test.updateData(data)

Obviously the dictionary is not updated at all using this method.
Is there a way to make this work properly?

Thank you in advance !

Member Avatar
snippsat
Veteran Poster
1,039 posts since Aug 2008
Reputation Points: 483 [?]
Q&As Helped to Solve: 381 [?]
Skill Endorsements: 10 [?]
 
0
 

Here something you can look at.

class testClass(object):
    def __init__(self):
        self.my_dict = {}
        
    def uppdate(self,key, value):
        self.my_dict[key] = value

    def remove(self,del_key):
        del self.my_dict[del_key]

    def show_data(self):
        print self.my_dict

if __name__ == '__main__':
    test = testClass()
    test.uppdate('a', 1)
    test.uppdate('b', 2)
    test.uppdate('c', 3)
    test.remove('c')
    test.show_data()  #{'a': 1, 'b': 2}

Something to think off is that a class uses dicitonary as a internal storage.
Example.

class Server(object): 
    '''python constructor'''
    def __init__(self, server_name, port):        
        self.server_name = server_name
        self.port = port

server_1 = Server('master_100', 1234)
server_2 = Server('blob_500', 9999)

#if we print the internal working of the class storage of variables
print server_1.__dict__
print server_2.__dict__
'''Out-->
{'port': 1234, 'server_name': 'master_100'}
{'port': 9999, 'server_name': 'blob_500'}
'''

#So it work like a dictionary if we call server_name(key)
#We are getting the value.
print server_1.server_name
#--> master_100
print server_2.port
#--> 9999
Member Avatar
sebcbien
Newbie Poster
10 posts since Jul 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you for your quick answer, I've continued my researches on this issue. It seems that we actually cannot make list, dict or tuple containing object or variable. I mean if I do :

a = ''
b = 4
c = dict( key = 0 )
l = [a, b, c]
print l
#['', 4, {'key': 0}]

a = 'a string'
b = 78
c['key'] = 49

print l 
# ['', 4, {'key': 49}]

if this was done in a logical way I should expect to have with the second print :


I really don't understand this behaviour of python, isn't it weird?

Member Avatar
snippsat
Veteran Poster
1,039 posts since Aug 2008
Reputation Points: 483 [?]
Q&As Helped to Solve: 381 [?]
Skill Endorsements: 10 [?]
 
0
 
>>> c = dict( key = 0 )
>>> c
{'key': 0}
>>> c['key'] = 49
>>> c
{'key': 49}
>>> my_list = []
>>> my_dict = {}
>>> a = 'a string'
>>> b = 78
>>> my_list.append(a)
>>> my_list.append(b)
>>> my_list
['a string', 78]
>>> my_list.append(c)
>>> my_list
['a string', 78, {'key': 49}]
>>>
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: