Which more Pythonic - self.__class__ or type(self)?

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Mar 2 17:53:28 EST 2023


On 3/03/23 9:54 am, Ian Pilcher wrote:
> I haven't found
> anything that talks about which form is considered to be more Pythonic
> in those situations where there's no functional difference.

In such cases I'd probably go for type(x), because it looks less
ugly.

x.__class__ *might* be slightly more efficient, as it avoids a
global lookup and a function call. But as always, measurement
would be required to be sure.

-- 
Greg


More information about the Python-list mailing list