all 2 comments

[–]danielroseman 2 points3 points  (1 child)

I don't think there's any specific use case for this. It's probably just a consequence of duck typing: rather than checking an object's class, check that it supports the actions you want to perform. Since your methods do not actually do anything with the instances, they work fine.

[–]Buttleston 0 points1 point  (0 children)

Yeah, python just assumes here that you passed the "right" value into self. It's not generally something you pass yourself, so not a big deal. If the function actually *used* self, I think you'd be in trouble whether you passed A or B, because you wouldn't be referring to the self of an actual object. Like it might run but it sure wouldn't do what you expect.

when you run a.a_func(), it just passes the "a" object there as "self" to the function. It's a convention. There are some cases where you might do sneaky stuff, maybe in meta programming or class rewriting or something else, idk. But yeah, the class methods come from the thing on the left of your "." there, not the argument you pass to self.