I am looking at an existing code base that is using a bool variable like a method, for example:
class Manager(object):
def __init__(self):
self._on_elect_callback = None
self._on_revoke_callback = None
self.... = ... (..., event_listener = self._event)
def _event(self, type):
on_elect_callback = self._on_elect_callback
if type == SOME_CONSTANT:
....
if on_elect_callback:
on_elect_callback()
def do_this(self, on_elect_function):
self._on_elect_callback = on_elect_function
if self....:
on_elect_function()
Questions:
I am curious how that
on_elect_callbackis being used like a function with()afterifcondition on the last line. Isn't that some boolean variable? I searched the repo and there is no definition for that. What is it doing?Also, I would like to set a variable in
__init__that the callback function of event can use like "hey this event type IS SOME_CONSTANT, so set the variable in__init__to"ABCD"(orTrue), how can I achieve it? Is the way in the code above the way to do it?
self._on_elect_callbackis set toNoneinitially, which is considered falsy, but presumably there muse be another method somewhere which assigns some sort of callable, which is considered truthy and will pass theif.