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

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    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__?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Don't put double-underscores in front of your attribute names if you don't want them mangled.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    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!

IMN logo majestic logo threadwatch logo seochat tools logo