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

  • Home
  • SEARCH
  • 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 8513535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:35:54+00:00 2026-06-11T04:35:54+00:00

Edit: I’m using Python 3 (some people asked). I think this is just a

  • 0

Edit: I’m using Python 3 (some people asked).

I think this is just a syntax question, but I want to be sure there’s nothing I’m missing. Notice the syntax difference in how Foo and Bar are implemented. They achieve the same thing and I want to make sure they’re really doing the same thing. The output suggests that there are just two ways to do the same thing. Is that the case?

Code:

class X:

    def some_method(self):
        print("X.some_method called")

class Y:

    def some_method(self):
        print("Y.some_method called")

class Foo(X,Y):

    def some_method(self):
        X().some_method()
        Y().some_method()
        print("Foo.some_method called")

class Bar(X,Y):

    def some_method(self):
        X.some_method(self)
        Y.some_method(self)
        print("Bar.some_method called")

print("=== Fun with Foo ===")
foo_instance = Foo()
foo_instance.some_method()

print("=== Fun with Bar ===")
bar_instance = Bar()
bar_instance.some_method()

Output:

=== Fun with Foo ===
X.some_method called
Y.some_method called
Foo.some_method called
=== Fun with Bar ===
X.some_method called
Y.some_method called
Bar.some_method called

PS – Hopefully it goes without saying but this is just an abstract example, let’s not worry about why I’d want to call some_method on both ancestors, I’m just trying to understand the syntax and mechanics of the language here. Thanks all!

  • 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-11T04:35:55+00:00Added an answer on June 11, 2026 at 4:35 am

    They aren’t the same. X() creates an object of class X. When you do X().someMethod() you create a new object and then call the method on that object, not on self. X.someMethod(self) is what you want, since that calls the inherited method on the same object.

    You will see the difference if your method actually does anything to the self object. For instance, if you put self.blah = 8 into your method, then after X.someMethod(self) the object you call it on will have the blah attribute set, but after X().someMethod() it will not. (Instead, you will have created a new object, set blah on that, and then thrown away that new object without using it, leaving the original object untouched.)

    Here is a simple example modifying your code:

    >>> class X:
    ... 
    ...     def some_method(self):
    ...         print("X.some_method called on", self)
    ... 
    ... class Y:
    ... 
    ...     def some_method(self):
    ...         print("Y.some_method called on", self)
    ... 
    ... class Foo(X,Y):
    ... 
    ...     def some_method(self):
    ...         X().some_method()
    ...         Y().some_method()
    ...         print("Foo.some_method called on", self)
    ... 
    ... class Bar(X,Y):
    ... 
    ...     def some_method(self):
    ...         X.some_method(self)
    ...         Y.some_method(self)
    ...         print("Bar.some_method called on", self)
    >>> Foo().some_method()
    ('X.some_method called on', <__main__.X instance at 0x0142F3C8>)
    ('Y.some_method called on', <__main__.Y instance at 0x0142F3C8>)
    ('Foo.some_method called on', <__main__.Foo instance at 0x0142F3A0>)
    >>> Bar().some_method()
    ('X.some_method called on', <__main__.Bar instance at 0x0142F3C8>)
    ('Y.some_method called on', <__main__.Bar instance at 0x0142F3C8>)
    ('Bar.some_method called on', <__main__.Bar instance at 0x0142F3C8>)
    

    Note that when I use Foo, the objects printed are not the same; one is an X instance, one is a Y instance, and the last is the original Foo instance that I called the method on. When Bar is used, it is the same object in each method call.

    (You can also use super in some cases to avoid naming the base classes explicitly; e.g., super(Foo, self).someMethod() or in Python 3 just super().someMethod(). However, if you have a need to directly call inherited methods from two base classes, super might not be a good fit. It is generally aimed at cases where each method calls super just once, passing control to the next version of the method in the inheritance chain, which will then pass it along to the next, etc.)

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

Sidebar

Related Questions

Edit: Seems numerous people think this is a dumb idea, so I would appreciate
EDIT The bare-bones version of this question is, if I have some object o
EDIT: I'm using MySQL, I found another post with the same question, but it's
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
EDIT: Simple version of the question: I want to create server variables in the
Edit: The below question was answered by this . I have a new updated
Edit: I'm looking for solution for this question now also with other programming languages.
EDIT Leaving this for posterity, but nearly a year later, to get down voted,
EDIT : I've gotten the famous question badge with this question, so I figured
Edit: I think I figured out the solution, but I'm still interested to know

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.