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

The Archive Base Latest Questions

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

Im a bit confused about inherited instance variables in ABCs. I have written an

  • 0

Im a bit confused about inherited instance variables in ABCs. I have written an example to show my confusion. Class A needs a list which class B inherits but it must be an instance object rather than a class object. However class B also needs its own instance variable local. Can anyone set me straight?

#!python
from abc import ABCMeta, abstractmethod, abstractproperty
import unittest

class A(object):

    __metaclass__ = ABCMeta
    _internal = ['initialized']

    @property
    def internal(self):
        return self._internal

    def get_a(self):
        return self._internal

    @abstractmethod
    def set_a(self, value):
        pass

class B(A):   
    def __init__(self):
       self.local = 'OK'


    def get_local(self):
        return self.local

    def set_a(self, value):
        self._internal.append(value)


class TestCase(unittest.TestCase):

    def test_implementation(self): 
        self.assertEqual(['initialized'], B().get_a() )  # this passes but for wrong reason
        b_used = B().set_a('used') 
        b_unused = B()

        print "b_used.get_a() should return ['initialized','used']"
        print "b_unused.get_a() should return ['initialized']"
        print "b_used.get_local() should equal b_unused.get_local() = 'OK'"

        self.assertEqual(['initialized'], b_unused.get_a())  # >> fails with ['initialized'] =! ['initialized', 'used']
        self.assertNotEqual(b_unused.get_a(), b_used.get_a())


if __name__ == "__main__":
    unittest.main()

The problem is that _internal is a class obj of class A. I need it to be an instance object of class B.

Thanks In advance

  • 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-30T18:21:27+00:00Added an answer on May 30, 2026 at 6:21 pm

    You should initialize instance attributes in __init__() and call the base class __init__() in B:

    class A(object):
        __metaclass__ = ABCMeta
        def __init__(self):
            self._internal = ['initialized']
        ...
    
    class B(A):   
        def __init__(self):
            A.__init__(self)
            self.local = 'OK'
        ...
    

    You should also fix your unit test:

    class TestCase(unittest.TestCase):
    
        def test_implementation(self): 
            self.assertEqual(['initialized'], B().get_a() )  # this passes but for wrong reason
            b_used = B()
            b_used.set_a('used') 
            b_unused = B()
            ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a bit confused about how to overload the stream operators for my class
I'm a bit confused about the object references. Please check the examples below: class
I'm a bit confused about mvc terminology. Using the question title as an example,
I am bit confused about how Java handle conversion. I have char array consist
i read Here and bit confused about the unregistering events. means i have button
I am a bit confused about the nth-of-type pseudo class, and how this is
i am a bit confused about threads. If i have a file server and
I'm a little bit confused about the custom UiViewController inheritance. For example if I
I am a bit confused about this. I have an R package that has
I'm a bit confused about the following. Given this class: public class SomeClassToBeCasted {

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.