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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:17:27+00:00 2026-06-03T12:17:27+00:00

I have the following (obviously simplified) class class A(object) def __init__(self, a): self.a =

  • 0

I have the following (obviously simplified) class

class A(object)
    def __init__(self, a):
        self.a = a
        self.b = 'b'
        # Other class attributes are added


class B(list):
    """
    Some customization of this class...
    """
    pass

BB = B([A(i) for i in range(10)])

I want to do

B.a

and get a list of all the a attributes from every contained item in B. I know in order to do this, I need to overwrite __getattr__, but I’m not sure the best way to implement this. This needs to be generic as B doesn’t know any of the attributes of A that may need to be accessed.

Can someone offer some advice on the implementation of this idea?

  • 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-03T12:17:28+00:00Added an answer on June 3, 2026 at 12:17 pm

    General Solution:

    If you wish for this to work across the board, then you can override __getattr__() as you thought:

    class A(object):
        def __init__(self, a):
            self.a = a
            self.b = a-1
    
    class B(list):
        """
        Some customization of this class...
        """
        def __getattr__(self, name):
            return (getattr(item, name) for item in self)
    
    bb = B([A(i) for i in range(10)])
    print(list(bb.a))
    print(list(bb.b))
    

    Giving us:

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8]
    

    Note that __getattr__() only gets called if the attribute doesn’t already exist. So, if you set bb.b to another value, you will get that instead:

    bb = B([A(i) for i in range(10)])
    bb.b = 5
    print(list(bb.a))
    print(bb.b)
    

    Gives us:

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    5
    

    Example to show the lack of need for B to know about it’s contents:

    >>> import datetime
    >>> b = B([datetime.date(2012, 1, 1), datetime.date(2012, 2, 2), datetime.date(2012, 3, 3)])
    >>> list(b.month)
    [1, 2, 3]
    

    Original Answer:

    The easiest way to do this is with a generator expression.

    class B(list):
        """
        Some customization of this class...
        """
    @property
    def a(self):
        return (item.a for item in self)
    

    This generator expression is the equivalent of:

    @property
    def a(self):
        for item in self:
            yield item.a
    

    I also used the property() builtin as a decorator to make B.a act as an attribute rather than a function.

    We can then do:

    bb = B([A(i) for i in range(10)])
    print(list(bb.a))
    

    and get:

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    

    You could use a list comprehension ([item.a for item in self]) if you definitely wanted a list rather than an iterator, but generally the iterator is more useful, and can be easily made into a list (as shown above).

    Note you could also do this even more simply by assigning the generator expression:

    class B(list):
            """
            Some customization of this class...
            """
        def __init__(self, *args):
            super(B, self).__init__(*args)
            self.a = (item.a for item in self)
    

    However, this means the generator will be exhausted after the first use, so I would advise against it.

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

Sidebar

Related Questions

I have the following setup (simplified, obviously): An abstract class, with an A object
If I have created the following Employee object (simplified)... public class Employee { public
I am writing some unit tests which I have simplified to the following: class
Let's say I have the following app.js (obviously very simplified): var express = require('express'),
I have the following situation (simplified): a.h: #include <boost/serialisation/serialisation.hpp> class B; using namespace std;
I have the following code <textarea class=input id=input onkeypress=ifenter(event,'<?php echo $id ?>></textarea> Obviously in
I have a Rails app with the following (simplified) models: # member.rb class Member
Background: Suppose I have the following obviously-incorrect PHP: try{ $vtest = ''; print(array_pop($vtest)); }catch(Exception
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have following simple class: @interface Article: NSObject { NSString *title; } @property (copy,

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.