pancakes don' sweat the details in this code too much at this point in the bookit' mostly previewintended for general comparison to closures only we'll explore classes in depth in part viand will look at specific operator overloading tools like __call__ in the point to notice here is that classes can make state information more obviousby leveraging explicit attribute assignment instead of implicit scope lookups in additionclass attributes are always changeable and don' require nonlocal statementand classes are designed to scale up to implementing richer objects with many attributes and behaviors while using classes for state information is generally good rule of thumb to followthey might also be overkill in cases like thiswhere state is single counter such trivial state cases are more common than you might thinkin such contextsnested defs are sometimes more lightweight than coding classesespecially if you're not familiar with oop yet moreoverthere are some scenarios in which nested defs may actually work better than classes--stay tuned for the description of method decorators in for an example that is far beyond this already well-stretched scopestate with function attributes and as portable and often simpler state-retention optionwe can also sometimes achieve the same effect as nonlocals with function attributes--user-defined names attached to functions directly when you attach user-defined attributes to nested functions generated by enclosing factory functionsthey can also serve as per-callmultiple copyand writeable statejust like nonlocal scope closures and class attributes such user-defined attribute names won' clash with names python creates itselfand as for nonlocalneed be used only for state variables that must be changedother scope references are retained and work normally cruciallythis scheme is portable--like classesbut unlike nonlocalfunction attributes work in both python and in factthey've been available since much longer than ' nonlocal because factory functions make new function on each call anyhowthis does not require extra objects--the new function' attributes become percall state in much the same way as nonlocalsand are similarly associated with the generated function in memory moreoverfunction attributes allow state variables to be accessed outside the nested functionlike class attributeswith nonlocalstate variables can be seen directly only within the nested def if you need to access call counter externallyit' simple function attribute fetch in this model here' final version of our example based on this technique--it replaces nonlocal with an attribute attached to the nested function this scheme may not seem as intuitive to some at first glanceyou access state though the function' name instead of as simple why nonlocalstate retention options