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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:46:58+00:00 2026-06-17T16:46:58+00:00

I have object1 which has many sub-objects in it. These sub-objects are accessed in

  • 0

I have object1 which has many sub-objects in it. These sub-objects are accessed in the form object1.subobject. I have a function which returns a list of sub-objects of the original object. All I would like to do is iterate through the list and access each sub-object. Something like this:

temp_list = listSubChildren(object1)  #Get list of sub-objects
for sub_object in temp_list:          #Iterate through list of sub-objects
    blah = object1.sub-object         #This is where I need help 
    #Do something with blah           #So that I can access and use blah

I looked at similar questions where people used dictionaries and getattr but couldn’t get either of those methods to work for this.

  • 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-17T16:46:59+00:00Added an answer on June 17, 2026 at 4:46 pm

    It seems to me that if your listSubChildren method is returning strings as you imply, you can use the builtin getattr function.

    >>> class foo: pass
    ... 
    >>> a = foo()
    >>> a.bar = 1
    >>> getattr(a,'bar')
    1
    >>> getattr(a,'baz',"Oops, foo doesn't have an attrbute baz")
    "Oops, foo doesn't have an attrbute baz"
    

    Or for your example:

    for name in temp_list:
        blah = getattr(object1,name)
    

    As perhaps a final note, depending on what you’re actually doing with blah, you might also want to consider operator.attrgetter. Consider the following script:

    import timeit
    import operator
    
    class foo(object):
        def __init__(self):
            self.a = 1
            self.b = 2
            self.c = 3
    
    def abc(f):
        return [getattr(f,x) for x in ('a','b','c')]
    
    abc2 = operator.attrgetter('a','b','c')
    
    f = foo()
    print abc(f)
    print abc2(f)
    
    print timeit.timeit('abc(f)','from __main__ import abc,f')
    print timeit.timeit('abc2(f)','from __main__ import abc2,f')
    

    Both functions (abc, abc2) do nearly the same thing. abc returns the list [f.a, f.b, f.c] whereas abc2 returns a tuple much faster, Here are my results — the first 2 lines show the output of abc and abc2 respectively and the 3rd and 4th lines show how long the operations take:

    [1, 2, 3]
    (1, 2, 3)
    0.781795024872
    0.247200965881
    

    Note that in your example, you could use getter = operator.attrgetter(*temp_list)

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

Sidebar

Related Questions

In a project here, we have an Object ClassTest, which has many sub objects:
I have a JPA object which has a many-to-many relationship like this: @Entity public
I have a managed object which acts as a playlist, it has a to-many
I have a hibernate object called User which has a many-to-many relationship with another
I have got a class which has many virtual properties lazy loaded public class
I have a user object represented in JPA which has specific sub-types. Eg, think
I have a task table, which has a sub category. The sub category is
I have a CoreData model with an object bar which has a to-many relationship
I have an object which has many bufferedimages in it, I want to create
I have some complicated object, such as a Cat, which has many properties, such

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.