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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:33:40+00:00 2026-06-02T14:33:40+00:00

I have this base class and subclass: class Event: def __init__(self, sr1=None, foobar=None): self.sr1

  • 0

I have this base class and subclass:

class Event:
    def __init__(self, sr1=None, foobar=None):
        self.sr1 = sr1
        self.foobar = foobar


# Event class wrappers to provide syntatic sugar
class TypeTwoEvent(Event):
    def __init__(self, level=None):
        self.sr1 = level

Later on, when I try to check the foobar attribute of a TypeTwoEvent instance, I get an exception. For example, testing this at the REPL:

>>> event = TypeTwoEvent()
>>> event.foobar
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'TypeTwoEvent' object has no attribute 'foobar'

I thought that the base class attributes would be inherited by the subclass and that creating an instance of a subclass would instantiate the base class (and thus invoke its constructor). Therefore, I expected the foobar attribute value to be defaulted to None.

Why do TypeTwoEvent instances not have a foobar attribute, even though Event instances do?

  • 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-02T14:33:49+00:00Added an answer on June 2, 2026 at 2:33 pm

    The subclass should be:

    class TypeTwoEvent(Event):    
        def __init__(self, level=None, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.sr1 = level
    

    Because __init__ is overridden, the base class’ __init__ code will only run if it is explicitly requested.

    Despite its strange name, __init__ is not specially treated. It gets called automatically after the object is created; but otherwise it’s an ordinary method, and ordinary inheritance rules apply.

    super().__init__(arguments, that, go, to, parents)
    

    is the syntax to call the parent version of the method. Using *args and **kwargs allows us to catch additional arguments passed to __init__ and pass them to the parent method; this way, when a TypeTwoEvent is created, a value can be specified for the foobar, along with anything else specific to the base class.

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

Sidebar

Related Questions

So I have this: class Parent(object): def __init__(self, val): print 'enter Base init' self._set_x(val)
I have a base class like this: class base(object): name = def __init__(self,name): self.user
I have this code in base class protected virtual bool HasAnyStuff<TObject>(TObject obj) where TObject:class
I have a class that inherits from a base class (this contains a lot
I have a base class Parent like this: using System; using System.Collections.Generic; using System.Text;
I have a base class like this: package MyClass; use vars qw/$ME list of
In my base class I have a generic method (ideally this would be a
Please Consider this scenario: We have a base class called clsMain : class clsMain
I have a declarative table defined like this: class Transaction(Base): __tablename__ = transactions id
the question is simple. I have a base abstract class (person). From this i

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.