Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6005061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:21:00+00:00 2026-05-23T01:21:00+00:00

How do I get the attribute names and values of an object? I’m attempting

  • 0

How do I get the attribute names and values of an object? I’m attempting to cast it to a dictionary so that I can easily JSON serialize the object.

Code:

class User:
    ...
    def to_dict( self ):
        dict = {}
        for key, value in filter( lambda aname: not aname.startswith('_'), dir(self) ):
            dict[key] = value
    return dict

Error:

too many values to unpack

Use:

user = User({
    's_email': 'bob@email.com',
    's_password': 'password',
})
user.to_dict()
# JSON Serialize
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:21:01+00:00Added an answer on May 23, 2026 at 1:21 am

    Use self.__dict__. It is a dictionary representing the namespace of the object.

    class User:
        ...
        def to_dict(self):
            return dict(
                [(k, v) for k, v in self.__dict__.iteritems() if not k.startswith('_')]
            )
    

    Note that given your code snippet, the dict returned by .to_dict() would contain the key 'to_dict' as the function does not start with an underscore. Probably a simple mistake in your post.

    If the list of attributes that you want to include in the returned dict is small (and doesn’t change much), I suggest being explicit about it and listing them

    class User(object):
        data_attributes = ('s_email', 's_password')
        def to_dict(self):
            return dict([(attr, getattr(self, attr) for attr in self.data_attributes])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know i can get the attribute names, types, and set their values from
Can anyone tell me how can I get the xpath of the name attribute
I have an attribute declared on property. How can I get the property name
I get this error object has no attribute 'im_func' with this class Test(object): def
In the following Jackson/Java code that serializes objects into JSON, I am getting this:
I am trying to get a attribute of a date picker to pull the
I'm inside a cfloop over a query. I want to get an attribute, but
How do I get the version attribute of a file? I tried the Version
I would like to get the AssemblyCompany attribute from a WinForm project inside of
I'm trying to get the source attribute of all images withing a specific div

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.