Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

+)

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?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading