In Python, can you call an instance method of class A, but pass in an instance of class B?

前端 未结 4 1782
轻奢々
轻奢々 2020-12-19 07:57

In the interest of reusing some existing code that was defined as an instance method of a different class, I was tying to do something like the following:

cl         


        
4条回答
  •  盖世英雄少女心
    2020-12-19 08:35

    It happens because python wraps class functions as an "unbound method" which performs this type checking. There's some description of the decisions involved in this here.

    Note that this type checking has actually been dropped in python 3 (see the note at the end of that article), so your approach will work there.

提交回复
热议问题