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 8266333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:07:20+00:00 2026-06-08T05:07:20+00:00

I have a python object with several attributes and methods. I want to iterate

  • 0

I have a python object with several attributes and methods. I want to iterate over object attributes.

class my_python_obj(object):
    attr1='a'
    attr2='b'
    attr3='c'

    def method1(self, etc, etc):
        #Statements

I want to generate a dictionary containing all of the objects attributes and their current values, but I want to do it in a dynamic way (so if later I add another attribute I don’t have to remember to update my function as well).

In php variables can be used as keys, but objects in python are unsuscriptable and if I use the dot notation for this it creates a new attribute with the name of my var, which is not my intent.

Just to make things clearer:

def to_dict(self):
    '''this is what I already have'''
    d={}
    d["attr1"]= self.attr1
    d["attr2"]= self.attr2
    d["attr3"]= self.attr3
    return d

·

def to_dict(self):
    '''this is what I want to do'''
    d={}
    for v in my_python_obj.attributes:
        d[v] = self.v
    return d

Update:
With attributes I mean only the variables of this object, not the methods.

  • 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-06-08T05:07:21+00:00Added an answer on June 8, 2026 at 5:07 am

    Assuming you have a class such as

    >>> class Cls(object):
    ...     foo = 1
    ...     bar = 'hello'
    ...     def func(self):
    ...         return 'call me'
    ...
    >>> obj = Cls()
    

    calling dir on the object gives you back all the attributes of that object, including python special attributes. Although some object attributes are callable, such as methods.

    >>> dir(obj)
    ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'bar', 'foo', 'func']
    

    You can always filter out the special methods by using a list comprehension.

    >>> [a for a in dir(obj) if not a.startswith('__')]
    ['bar', 'foo', 'func']
    

    or if you prefer map/filters.

    >>> filter(lambda a: not a.startswith('__'), dir(obj))
    ['bar', 'foo', 'func']
    

    If you want to filter out the methods, you can use the builtin callable as a check.

    >>> [a for a in dir(obj) if not a.startswith('__') and not callable(getattr(obj, a))]
    ['bar', 'foo']
    

    You could also inspect the difference between your class and its instance object using.

    >>> set(dir(Cls)) - set(dir(object))
    set(['__module__', 'bar', 'func', '__dict__', 'foo', '__weakref__'])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following situation in my python code: class Parent(object): def run(self): print
I have a Python datetime object that I want to convert to unix time,
I want a Python object that will monitor whether other objects have changed since
I have the following simple methods for writing a python object to a file
I have a Python file with as content: import re import urllib class A(object):
The title says it mostly. If I have an object in Python and want
I have the following code. class person(object): def __init__(self, keys): for item in keys:
Newbie Python question. I have a class that inherits from several classes, and some
say I have a python object o with a method m(), and I want
I have a Python application in the bottle web-server that accesses a C shared-object

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.