0

I ran this code

class A:
    def __init__(self, func) -> None:
        self.func = func

class B(A, int):
    pass

b = B(lambda t: t)

and got this error:

TypeError                                 Traceback (most recent call last)
Cell In[40], line 8
      5 class B(A, int):
      6     pass
----> 8 b = B(lambda t:t)

TypeError: int() argument must be a string, a bytes-like object or a real number, not 'function'

What is the cause of the error? An empty class instead of int works.

+)

class A:
    def __init__(self, func) -> None:
        self.func = func

class C:
    def __init__(self, s) -> None:
        assert type(s) == str

class B(A, C):
    pass

b = B(lambda t: t)

this code works, so I am curious that what is special in int class, since the constructor never runs.

float
  • 23
  • 3
  • 1
    Related: https://stackoverflow.com/questions/2673651/inheritance-from-str-or-int and https://stackoverflow.com/questions/16843158/python-multiple-inheritance-of-new-and-init-with-a-string-and-second-cla – slothrop Aug 23 '23 at 12:20
  • 1
    Remember that when a class's `__new__` is implemented, it's called to return an object, and only after that is that object's `__init__` called. So if `B`'s `__new__` doesn't give an instance of `B` then `B.__init__` is never called. – slothrop Aug 23 '23 at 12:30
  • Thank you so much!! Now I understood the reason. – float Aug 23 '23 at 12:35

2 Answers2

0

You define class B to inherit from class A and from int. Class A constructs from a function,as stated in the __init__ method, but int constructs from a number. When you try instantiate B with the lambda function, the int constructor raises a TypeError. Are you sure you need B to inherit from both A and int?

jlgarcia
  • 333
  • 6
  • -1 this answer misses what's actually going on here. It sounds like you're suggesting that all base class `__init__`s get with the same arguments for `C`, but that's not at all how Python works. If you replace `int` with a different "ordinary" type, then the OP's code would work _only_ `A.__init__` would be called. – Brian61354270 Aug 23 '23 at 13:00
  • @Brian61354270 Never talked about class `C` because it was an edit made after I posted my answer. – jlgarcia Aug 24 '23 at 05:25
  • Whether the class is named `B` or `C` doesn't matter, this still isn't correct. You can following the links from slothrop in the comments above to learn about how object initialization actually works and why this behavior happens. – Brian61354270 Aug 24 '23 at 13:01
-1

The error occurs because you're inheriting from both class A and class int, leading to issues with the int constructor. If you want class B to have an integer value and inherit from A, consider using composition instead of directly inheriting from int.

Vrijdag
  • 39
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 25 '23 at 13:32