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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:56:46+00:00 2026-05-24T02:56:46+00:00

I have two classes like the following: class Super(object): def __init__(self, arg): self.arg =

  • 0

I have two classes like the following:

class Super(object):
    def __init__(self, arg):
        self.arg = arg

    def load(self):
        # Load data from disk if available
        try:
            self.data = load_routine()
        except IOError as e:
            if e[0] == errno.ENOENT:
                pass
            else:
                raise


class Sub(Super):
    def __init__(self, arg2, *args, **kwargs):
        super(Sub, self).__init__(*args, **kwargs)
        self.arg2 = arg2

    def load(self):
        # Load files specific to superclass
        super(Sub, self).load()
        # Load data from disk if available
        try:
            self.data2 = another_different_load_routine(self.arg2)
        except IOError as e:
            if e[0] == errno.ENOENT:
                pass
            else:
                raise

I hope the code is clear enough, and unless I missed something this should work for both classes by using it for example like: obj = Super(); obj.load().

However, I actually want the load method to be called automatically at the end of __init__(), but I am not sure on how to accomplish this. If I add self.load() at the end of __init__() for both classes the load() method of the subclass would be called twice, if try to create an instance of Sub, once from the superclass and once from the subclass.

If I instead put the call to self.load() only at the end of the init method of the superclass, it will only be called once but then the object hasn’t been initialized to contain the attributes it needs for loading.

What I do want, when instantiating Sub, is for it to initialize all attributes in __init__ for both super and subclass, and call load() for both super and subclass. Whether its in the order

Super -> __init__(), Super -> load(), Sub -> __init__(), Sub -> load()

or

Super -> __init__(), Sub -> __init__(), Super -> load(), Sub -> load()

is not important. How can I accomplish this?

  • 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-24T02:56:46+00:00Added an answer on May 24, 2026 at 2:56 am

    The answer is the combination you failed to enumerate:

    class Super(object):
        def __init__(self):
            print 'init super'
            if self.__class__ == Super:
                self.load()
        def load(self):
            print 'load super'
    
    class Sub(Super):
        def __init__(self):
            # always do super first in init
            super(Sub, self).__init__()
            print 'init sub'
            self.load()
        def load(self):
            # load is essentially an extension of init
            # so you still need to call super first
            super(Sub, self).load()
            print 'load sub'
    
    sub = Sub()
    

    If you actually want to instantiate super (it’s not an abstract class) you need the if test in it’s init. Otherwise, you cannot get the in-order semantics you want with your current class structure and initialization scheme.

    Sub() will call Sub.__init__, which will call Super.__init__ (before doing anything).

    Afterwards, __init__ on Sub will do it’s thing.

    Last, Sub.__init__ will call Sub.load, which will call Super.load (before doing anything), then do it’s own work.

    The “normal” way to do this would be

    sub = Sub()
    sub.load()
    sup = Super()
    sup.load()
    

    Without the __init__ methods calling load at all. This is probably what I’d recommend if you really want to call up levels in load as it is essentially a second set of __init__s.

    Edit: Read the comments about the failings in the previous two versions (and see the edits to see the older one). Here is another version, using a metaclass:

    class Loader(type):
        def __new__(cls, name, bases, attrs):
            if attrs.get('__init__'):
                attrs['_init'] = attrs['__init__']
                del attrs['__init__']
            if attrs.get('_init_'):
                attrs['__init__'] = lambda self: self._init_()
                attrs['_init'] = lambda self: None
            return super(Loader, cls).__new__(cls, name, bases, attrs)
    
    class Super(object):
        __metaclass__ = Loader
        def _init_(self):
            print 'init super'
            self._init()
            self.load()
    
        def load(self):
            print 'load super'
    
    class Sub(Super):
        def __init__(self):
            print 'init sub'
    
        def load(self):
            super(Sub, self).load()
            print 'load sub'
    
    
    sub = Sub()
    sup = Super()
    

    This has a different restriction: All subclasses can act perfectly normally, except they can’t call Sub.__init__, by using super().__init__(). I think it’s possible to remove this restriction but I don’t see how at this second without another layer of indirection somewhere.

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

Sidebar

Related Questions

I have two classes: class Activity < ActiveRecord::Base belongs_to :activity_type def belongs_to_cat_a? self.activity_category ==
I have two classes declared like this: class Object1 { protected ulong guid; protected
I have two classes, Foo and Bar, that have constructors like this: class Foo
Let's say I have the following two classes: public class TestResults { public string
I have two classes, and want to include a static instance of one class
I have two classes: Action and MyAction . The latter is declared as: class
I have two classes in my app class A and Class B. Both class
Say I have two classes: class A(db.Model): class B(db.Model): a_reference = ReferenceProperty(A) I can
How do I model the following using Castle ActiveRecord? I have two classes, Customer
I have two classes with a parent-child relationship (the Parent class has-a Child class),

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.