to use module-level variables instead of singletonwe instantiate an instance of the class after we've defined it we can improve our state pattern to use singletons instead of creating new object every time we change stateswe can create module-level variable that is always accessibleclass firsttagdef process(selfremaining_stringparser)i_start_tag remaining_string find('<'i_end_tag remaining_string find('>'tag_name remaining_string[i_start_tag+ :i_end_tagroot node(tag_nameparser root parser current_node root parser state child_node return remaining_string[i_end_tag+ :class childnodedef process(selfremaining_stringparser)stripped remaining_string strip(if stripped startswith("</")parser state close_tag elif stripped startswith("<")parser state open_tag elseparser state text_node return stripped class opentagdef process(selfremaining_stringparser)i_start_tag remaining_string find('<'i_end_tag remaining_string find('>'tag_name remaining_string[i_start_tag+ :i_end_tagnode node(tag_nameparser current_nodeparser current_node children append(nodeparser current_node node parser state child_node return remaining_string[i_end_tag+ :class textnodedef process(selfremaining_stringparser)i_start_tag remaining_string find('<'text remaining_string[:i_start_tagparser current_node text text parser state child_node