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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:24:13+00:00 2026-06-09T11:24:13+00:00

A question about whether or not I’m going about something in the best way…

  • 0

A question about whether or not I’m going about something in the best way…

I would like to have a class hierarchy in Python that looks (minimally) like the following;

class Actor
  class Mover(Actor)
  class Attacker(Actor)
    class Human(Mover, Attacker)

But I run up against the fact that Actor has a certain attribute which I’d like to initialise, from each of the Mover and Attacker subclasses, such as in below;

class Actor:
    _world = None
    def __init__(self, world):
        self._world = world

class Mover(Actor):
    _speed = 0
    def __init__(self, world, speed):
        Actor.__init__(self, world)
        self._speed = speed

class Attacker(Actor):
    _range = 0
    def __init__(self, world, range):
        Actor.__init__(self, world)
        self._range = range

If I was then to go with my initial approach to this, and follow what I always have in terms of using superclass’ constructors, I will obviously end up calling the Actor constructor twice – not a problem, but my programmer sense tingles and says I’d rather do it a cleaner way;

class Human(Mover, Attacker):
    def __init__(self, world, speed, range):
        Mover.__init__(self, world, speed)
        Attacker.__init__(self, world, range)

I could only call the Mover constructor, for example, and simply initialise the Human‘s _range explicitly, but this jumps out at me as a much worse approach, since it duplicates the initialisation code for an Attacker.

Like I say, I’m aware that setting the _world attribute twice is no big deal, but you can imagine that if something more intensive went on in Actor.__init__, this situation would be a worry. Can anybody suggest a better practice for implementing this structure in Python?

  • 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-09T11:24:17+00:00Added an answer on June 9, 2026 at 11:24 am

    What you’ve got here is called diamond inheritance. The Python object model solves this via the method resolution order algorithm, which uses C3 linearization; in practical terms, all you have to do is use super and pass through **kwargs (and in Python 2, inherit from object):

    class Actor(object):    # in Python 3, class Actor:
        _world = None
        def __init__(self, world):
            self._world = world
    
    class Mover(Actor):
        _speed = 0
        def __init__(self, speed, **kwargs):
            super(Mover, self).__init__(**kwargs)    # in Python 3, super().__init__(**kwargs)
            self._speed = speed
    
    class Attacker(Actor):
        _range = 0
        def __init__(self, range, **kwargs):
            super(Attacker, self).__init__(**kwargs) # in Python 3, super().__init__(**kwargs)
            self._range = range
    
    class Human(Mover, Attacker):
        def __init__(self, **kwargs):
            super(Human, self).__init__(**kwargs)    # in Python 3, super().__init__(**kwargs)
    

    Note that you now need to construct Human with kwargs style:

    human = Human(world=world, range=range, speed=speed)
    

    What actually happens here? If you instrument the __init__ calls you find that (renaming the classes to A, B, C, D for conciseness):

    • D.__init__ calls B.__init__
      • B.__init__ calls C.__init__
        • C.__init__ calls A.__init__
          • A.__init__ calls object.__init__

    What’s happening is that super(B, self) called on an instance of D knows that C is next in the method resolution order, so it goes to C instead of directly to A. We can check by looking at the MRO:

    >>> D.__mro__
    (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <type 'object'>)
    

    For a better understanding, read Python’s super() considered super!

    Note that super is absolutely not magic; what it does can be approximated in Python itself (here just for the super(cls, obj) functionality, using a closure to bypass __getattribute__ circularity):

    def super(cls, obj):
        mro = type(obj).__mro__
        parent = mro[mro.index(cls) + 1]
        class proxy(object):
            def __getattribute__(self, name):
                return getattr(parent, name).__get__(obj)
        return proxy()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about whether or not a specific way of applying of
I have a question about what would be the best practice for creating a
I was going to ask a question here about whether or not my design
Disclaimer This is not a question about whether we should be escaping for database
Caveats: Let me first clarify that this is not a question about whether to
I have an interesting question about the way PHP evaluates boolean expressions. When you
I just posted a question about whether WPF is a good choice for a
Simple question: Office debate about whether the keyword AS is necessary in our T-SQL
perhaps a simple question, but we are discussing about whether it better to use
This question is about verifying the assembly to check whether it is tampered for

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.