for buttonseven though the gui expects to be able to invoke event handlers as simple functions with no argumentshandlers cb callback('blue'cb callback('green' button(command=cb button(command=cb remember blue remember green register handlers when the button is later pressedthe instance object is called as simple function with no argumentsexactly like in the following calls because it retains state as instance attributesthoughit remembers what to do--it becomes stateful function objectevents cb (cb (prints 'turn blueprints 'turn greenin factmany consider such classes to be the best way to retain state information in the python language (per generally accepted pythonic principlesat leastwith oopthe state remembered is made explicit with attribute assignments this is different than other state retention techniques ( global variablesenclosing function scope referencesand default mutable arguments)which rely on more limited or implicit behavior moreoverthe added structure and customization in classes goes beyond state retention on the other handtools such as closure functions are useful in basic state retention roles tooand ' nonlocal statement makes enclosing scopes viable alternative in more programs we'll revisit such tradeoffs when we start coding substantial decorators in but here' quick closure equivalentdef callback(color)def oncall()print('turn'colorreturn oncall enclosing scope versus attrs cb callback('yellow'cb (handler to be registered on eventprints 'turn yellowbefore we move onthere are two other ways that python programmers sometimes tie information to callback function like this one option is to use default arguments in lambda functionscb (lambda color='red''turn colordefaults retain state too print(cb ()the other is to use bound methods of class- bit of previewbut simple enough to introduce here bound method object is kind of object that remembers both the self instance and the referenced function this object may therefore be called later as simple function without an instance operator overloading