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

  • Home
  • SEARCH
  • 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 6819849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:21:18+00:00 2026-05-26T21:21:18+00:00

consider the following python code class Base(object): def __init__(self): self.foo = 5 class Derived(Base):

  • 0

consider the following python code

class Base(object):
    def __init__(self):
      self.foo = 5

class Derived(Base):
    def __init__(self):
      Base.__init__(self)
       self.foo = 6

bar = Base()
print bar.foo

foobar = Derived()
print foobar.foo

Can I access the foo in the base class from foobar. In C++ we can use Base::, how about Python? Any simple way?
Thanks

  • 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-26T21:21:19+00:00Added an answer on May 26, 2026 at 9:21 pm

    You can do this by using class attributes rather than instance attributes:

    class Base(object):
        foo = 5
    
    class Derived(Base):
        """A class derived from Base.
    
        >>> bar = Base()
        >>> print bar.foo
        5
        >>> foobar = Derived()
        >>> print foobar.foo
        6
        >>> print foobar.__class__.__base__.foo
        5
        >>> foobar.__class__.__base__.foo = 7
        >>> bar.foo
        7
        """
        foo = 6
    

    It’s important to understand the distinction. The code in your question creates classes that, when instantiated, grant to the newly created instance attributes of foo equal to 5 or 6. This code, on the other hand, creates foo attributes of the classes themselves that are accessible from any instances thereof.

    This is actually implemented by using a dictionary for each instance, and another dictionary for each ancestor class. If Python doesn’t find a requested attribute in the instance dictionary, it looks in the class dictionary for that instance; if it’s not not found there, Python will continue to search each base class for that attribute.

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

Sidebar

Related Questions

Consider the following Python (3.x) code: class Foo(object): def bar(self): pass foo = Foo()
In Python, consider I have the following code: class SuperClass(object): def __init__(self, x): self.x
consider the following python code: import gtk class MainWindow(): def __init__(self): self.window = gtk.Window()
Consider the following code in Python: class A(object): CLASS_ATTRIBUTE = 42 def f(self): return
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Consider the following example code class A: def __init__(self, i): self.i = i print("Initializing
Consider the following code: class Base(object): @classmethod def do(cls, a): print cls, a class
Consider the following code: class MyCustomDescriptor: def __init__(self,foo): self._foo = foo def __call__(self,decorated_method): #
Consider the following class: class SquareErrorDistance(object): def __init__(self, dataSample): variance = var(list(dataSample)) if variance

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.