1

So, I have this class which keeps track of the number of instances that are created:

class Base(object):
 total = 0
 def __init__(self):
 <more code>
 Base.total += 1

Now, I want to create a derived class, but I don't want its instances to add to the total in Base. So I subtract in order to undo the adding:

class Derived(Base):
 def __init__(self):
 super(Derived,self).__init__()
 Base.total -= 1

It works, however, it does not seem to me a good practice to access a base class attribute in the derived class. Is there a better method?

asked Apr 13, 2016 at 12:25
1
  • You don't have to call the __init__ method of your base class. You can always move your <more code> block into a separate function. Commented Apr 13, 2016 at 12:30

1 Answer 1

1

You can avoid calling superclass constructor in child class. It works fine but not looking very nice

noinspection PyMissingConstructor

class Derived(Base):

def __init__(self):
 <more code>
answered Apr 13, 2016 at 12:51
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.