December 14th, 2012, 08:27 PM
-
Converting python classes to dictionaries
I need to convert a class to json, so I was first planning on converting it to a dictionary. One thing I noticed when I use:
user_dict = user.__dict__
is that my properties are then labeled _User__name as opposed to just name. This is kind of annoying because I have properties that are just variables right now and if I decide to convert them to properties later this could break code. Is there a way to have this work short of manually creating the dictionary or looping through the results and removing _User__?
December 14th, 2012, 11:09 PM
-
Don't put double-underscores in front of your attribute names if you don't want them mangled.
December 15th, 2012, 01:13 PM
-
Reply
Originally Posted by Nyktos
Don't put double-underscores in front of your attribute names if you don't want them mangled.
I was using pydev's generator and I didn't know what I was doing wrong. Got it figured out now thanks for your help!