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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:49:58+00:00 2026-05-17T21:49:58+00:00

Please forgive the bad title – I had a hard time trying to think

  • 0

Please forgive the bad title – I had a hard time trying to think of a concise way to explain this.

I have a Python class that will have some underlying objects of other classes. I want to be able to create these underlying objects via a method of the original object. Let me try to explain better with an example:

class Foo:
    def __init__(self):
        self.bars = []

    def Bar(self, a, b, c):
        self.bars.append(Bar(a, b, c))

class Bar:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

I would use the above as such:

f = Foo()
f.Bar(1, 2, 3)

So this works how I want but is kind of crappy with respect to maintenance. Is there a nice “Pythonic” way to do this that would make maintaining this easy? For instance, let’s say I changed the constructor of Bar to:

__init__(self, a, b, c, d):

would there be a way to define all of this so I don’t have to update the argument list in 3 places?

  • 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-17T21:49:58+00:00Added an answer on May 17, 2026 at 9:49 pm

    Sure, no problem: Just pass *args and **kwargs on to Bar:

    class Foo:
        def __init__(self):
            self.bars = []
    
        def append_bar(self, *args, **kwargs):
            self.bars.append(Bar(*args, **kwargs))
    
    class Bar:
        def __init__(self, a, b, c, d):
            self.a = a
            self.b = b
            self.c = c
            self.d = d
    
    f=Foo()
    f.append_bar(1,2,3,4)
    

    PS. I changed the name of the method to append_bar because the usual convention in Python is to use lowercase names for methods, and I think methods whose names are verbs help describe what the method does.

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

Sidebar

Related Questions

Please forgive my long question. I have an idea for a design that I
I'm new to jquery and asp.net so please forgive if this is an obvious
I'm a very novice OCaml programmer so please forgive me if this is a
I have just started to look at .NET 3.5 so please forgive me if
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Please bear with me, I'm just learning C++. I'm trying to write my header
Please explain to me why the very last echo statement is blank? I expect
Please suggest some good resources to start writing Java Web services.
Please read the whole question. I'm not looking for an approach to managing multi-lingual
Please give me the direction of the best guidance on the Entity Framework.

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.