equality - Bound function object identity in Python -
this bit of python has me puzzled.
class my_class(object): def f(): return 1 c = my_class() f1 = c.f f2 = c.f assert not f1 f2 assert id(f1) != id(f2) assert f1 == f2
all 3 asserts pass. first 2 equivalent each other, expected both fail.
this seems strange me. python creating new, bound, member function objects whenever call c.f
? why that?
ok - looking here, guess answer first question yes. still, business of having different ids consistent ==
seems strange in context. containers, sure, want divergence between is
, ==
(i.e 2 distinct container objects contain equivalent data). member functions, seems is
, ==
ought equivalent.
can motivate bit of python design me?
edit : upon further reflection, makes sense me. in order memory footprint of object small, bound functions have created upon request, , distinct objects each request.
Comments
Post a Comment