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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:22:41+00:00 2026-06-09T15:22:41+00:00

I have a class Foo which contains a datamember of type Bar . I

  • 0

I have a class Foo which contains a datamember of type Bar. I can’t make a generalized, “default” Bar.__init__() – the Bar object is passed into the Foo.__init__() method.

How do I tell Python that I want a datamember of this type?


class Foo:

# These are the other things I've tried, with their errors    
    myBar         # NameError: name 'myBar' is not defined
    Bar myBar     # Java style: this is invalid Python syntax.
    myBar = None  #Assign "None", assign the real value in __init__. Doesn't work 
#####

    myBar = Bar(0,0,0) # Pass in "default" values. 
    def __init__(self, theBar):
        self.myBar = theBar

    def getBar(self):
        return self.myBar

This works, when I pass in the “default” values as shown. However, when I call getBar, I do not get back the one I passed in in the Foo.__init__() function – I get the “default” values.


b = Bar(1,2,3)
f = Foo(b)
print f.getBar().a, f.getBar().b, f.getBar().c

This spits out 0 0 0, not 1 2 3, like I’m expecting.

If I don’t bother declaring the myBar variable, I get errors in the getBar(self): method (Foo instance has no attribute 'myBar').

What’s the correct way to use a custom datamember in my object?

  • 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-09T15:22:42+00:00Added an answer on June 9, 2026 at 3:22 pm

    You don’t need to tell Python you are going to add a certain data member – just add it. Python is more dynamic than e.g. Java in this regard.

    If bar instances are essentially immutable (meaning they are not changed in practice), you can give the default instance as default value of the __init__() parameter:

    class Foo:
        def __init__(self, the_bar=Bar(0,0,0)):
            self.my_bar = the_bar
    

    All Foo instances uisng the default value will share a single Bar instance. If the Bar instance might be changed, this is probably not what you want, and you should use this idiom in this case:

    class Foo:
        def __init__(self, the_bar=None):
            if the_bar is None:
                the_bar = Bar(0,0,0)
            self.my_bar = the_bar
    

    Note that you shouldn’t usually write getters and setters in Python. They are just unnecessary boilerplate code slowing down your application. Since Python supports properties, you also don’t need them to be future-proof.

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

Sidebar

Related Questions

I have a class MyClass , which contains two member variables foo and bar
I have a class MyClass , which contains two member variables foo and bar
Let's say we have a class foo which has a private instance variable bar
Imagine I have a C++ class Foo and a class Bar which has to
I have a base class Foo that is concrete and contains 30 methods which
I have a class, let's call it Foo , which contains the 3 following
I have some Foo objects which are contained in Bar objects: class Foo {
I have inherited code which contains static nested classes as: public class Foo {
I have a class(let's call it foo ) which contains 20 members. The class
I have a class Foo, which has a (simple) destructor. Some other class contains

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.