mardi 14 octobre 2014

Changing dictionary keys in class object


Vote count:

0




Let's assume I have the following class:



class Test:
def __init__(self, a, b):
self.a = a
self.b = b


And I can convert that class to dictionary like this:



>>> a = Test(1,2)
>>> a.__dict__
{'a': 1, 'b': 2}


Now, I want to change the key of that dictionary to some other thing:



>>> a.__dict__
{'new_a': 1, 'new_b': 2}


Is there a proper way of achieving this by adding some new method in the class Test such that it will automatically covert them to the desired output ?



asked 1 min ago

Sibi

10.5k






Changing dictionary keys in class object

Aucun commentaire:

Enregistrer un commentaire