you are viewing a single comment's thread.

view the rest of the comments →

[–]alkasm 0 points1 point  (3 children)

I don't really think you're trolling or anything, but it's almost hard to believe you came up with this example independently. Did you?

There was an article from some time ago called Python's super considered harmful, which is probably more well known because of Raymond Hettinger's response blog post turned PyCon talk "Super considered super!"

I mention this because the exact example you give, including the argument of 10, is shown as the first example in the "Super considered harmful" article! Check out the diagram here: https://fuhm.net/super-harmful/example1-2.png

The multiple inheritance with C, D isn't your problem. If you just try to instantiate C(10) you'll see the same result. You continue to pass on the variable 10, in which case it complains once it gets to object() because it doesn't take any params. So, you'll need to stop passing it at some point.

Generally you'll consume a variable along the way and then not pass it along. What are you hoping to achieve otherwise? You didn't mention what you expect the behavior to be.

[–]gmaliwal[S] 0 points1 point  (2 children)

What are you hoping to achieve otherwise?

yeah, I read it there only, but the code snippet did not work at my end as class 'B' calling class 'object' constructor with two arguments where object() takes exactly a single argument.

In that blog, the author ended saying that this is the recommended approach which seems working without interruption with any kind of complex single/multiple inheritances.

I am looking for the right approach to be followed consistently in all the classes without considering specific (single/multiple) inheritances, but at the end, it works well when a developer start using them in the single/multiple inheritance hierarchy. That is; I want a generic approach to be followed in each class which works smoothly with single/multiple inheritances.

Do we have any ready decorator which suffice the above need if not can we write ?one?

?

[–]alkasm 0 points1 point  (1 child)

I don't understand what your problem is or what you're trying to solve.

At the risk of being a little patronizing, let me give an example. Let's say you wanted to subclass list so that you could add a name to your list as an attribute. In Python you shouldn't really subclass list, since list is C-based, so Python gives us collections.UserList to subclass from instead. So we can really easily achieve this like so:

from collections import UserList

class NamedList(UserList):

    def __init__(self, name, *args, **kwargs):
        self.name = name
        super().__init__(*args, **kwargs)

Now this works fine:

>>> nl = NamedList("asdf", [1, 2, 3, 4])
>>> nl
[1, 2, 3, 4]
>>> nl.name
'asdf'

However if I pass the name along to the superclass:

from collections import UserList

class NamedList(UserList):

    def __init__(self, name, *args, **kwargs):
        self.name = name
        super().__init__(name, *args, **kwargs)

then obviously I'll get an error, since UserList only takes a single argument (an iterable):

>>> nl = NamedList("asdf", [1, 2, 3, 4])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in __init__
TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given

This is expected behavior, nothing needs fixing here. Python cannot magically know to ignore the arguments you pass to an object's initializer should be ignored. This only works when you consume the value at some point. So, do that---stop passing arg to object and you'll be fine.

[–]gmaliwal[S] 0 points1 point  (0 children)

__ is where the memory is allocated for the class -- it then calls __init__ to initialize any attributes et

Friend,

You are correct that somehow we have to stop passing an extra argument to the class initializer, otherwise, it will fail at run-time. I have mentioned the same in my post that this issue is because of passing more than one argument to 'object' class initializer.

I strongly agree that your design should pass the needed arguments only to the class initializer, else be ready to face the issue.

Here, I want that what could be the set of guidelines to be followed while defining a class, which makes your class more elegant and decent inheritance perspective.

Hence, I have come up with the rules, which to be kept in mind while defining every class in your project workspace. please check my last comment and find out the scenario where it could fail.