What is the cause of the error during inheriting int class?

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.

>Solution :

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?

Leave a Reply