After initializing an object attribute to point to an object method, the variable will not be evaluated as being the same (using 'is') as the object method. It will still be evaluated as having equality (==) with the method.
class SomeClass:
def __init__(self):
self.command = self.do_something
def do_stuff(self):
print('c1:', id(self.command))
print('c2:', id(self.do_something))
if self.command is self.do_something:
return 'Commands match (is)'
elif self.command == self.do_something:
return 'Commands match (==)'
return 'Commands do not match at all'
def do_something(self):
pass
some_object = SomeClass()
print(some_object.do_stuff())
print()
def some_func():
pass
command = some_func
print('c1:', id(some_func))
print('c2:', id(command))
if some_func is command:
print('Commands match (is)')
else:
'Commands do not match at all'
I was expecting the ids of self.command and self.do_something to be the same just like some_func and command ids are the same.
self.do_somethingreturns a new bound method object every time you access itelif self.command == self.do_something: