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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:44:59+00:00 2026-06-05T06:44:59+00:00

I have a Python class that stores some fields and has some properties, like

  • 0

I have a Python class that stores some fields and has some properties, like

class A(object):
  def __init__(self, x, y):
    self.x = x
    self.y = y

  @property
  def z(self):
    return self.x+1

What changes do I need to make to the class so that I can do

>>> a = A(1,5)
>>> dict(a)
{'y':5, 'z':2}

where I specify that I want to return y and z? I can’t just use a.__dict__ because it would contain x but not z. I would like to be able to specify anything that can be accessed with __getattribute__.

  • 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-05T06:45:00+00:00Added an answer on June 5, 2026 at 6:45 am

    Add an __iter__() method to your class that returns an iterator of the object’s items as key-value pairs. Then you can pass your object instance directly to the dict() constructor, as it accepts a sequence of key-value pairs.

    def __iter__(self):
        for key in "y", "z":
            yield key, getattr(self, key)
    

    If you want this to be a little more flexible, and allow the list of attributes to be easily overridden on subclasses (or programmatically) you could store the key list as an attribute of your class:

    _dictkeys = "y", "z"
    
    def __iter__(self):
        for key in self._dictkeys:
            yield key, getattr(self, key)
    

    If you want the dictionary to contain all attributes (including those inherited from parent classes) try:

    def __iter__(self):
        for key in dir(self):
            if not key.startswith("_"):
                value = getattr(self, key)
                if not callable(value):
                    yield key, value
    

    This excludes members that start with “_” and also callable objects (e.g. classes and functions).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a Python class that I want to add an extra property
I have written a class in python that implements __str__(self) but when I use
In Python, say I have some class, Circle, that inherits from Shape. Shape needs
I have a python class I want to instantiate and the __init__ definition has
I have a class that stores all created references in a static list like
I'm working on some Django-code that has a model like this: class Status(models.Model): code
Possible Duplicate: Circular (or cyclic) imports in Python I have class B that imports
I have a Python file with as content: import re import urllib class A(object):
I have a program in python that includes a class that takes a function
I have a python class which reads a config file using ConfigParser: Config file:

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.