unregistering a class
Consult python docs for a better explanation

Since there is no main() function in Python, when the command to run a
python program is given to the interpreter, the code that is at level
0 indentation is to be executed. However, before doing that, it will
define a few special variables. __name__ is one such special variable.
If the source file is executed as the main program, the interpreter
sets the __name__ variable to have a value “__main__”.


If this file is
being imported from another module, __name__ will be set to the
module’s name.
__name__ is a built-in variable which evaluates to the name of the current module. Thus it can be used to check whether the current
script is being run on its own or being imported somewhere else by
combining it with if statement, as shown below.

if __name__ == &quot;__main__&quot;:
    register()

which when run from text editor in above case registers the class OBJECT_OT_add_object  When run again it has &quot;no idea&quot; that it is the same class as registered before. ( Hence the error if you try to unregister)
If however the class is registered when enabled as an addon it will be in that addon modules namespace my_addon.OBJECT_OT_add_object (ie __name__ == &quot;my_addon&quot;) and will be the same class when unregistered from the addon.
This is included to test scripts in the text editor.  Removing it will have no consequence if installed as an addon.
Have a feeling an issue here is:
When testing such scripts it has the tendency to append and append and append the item to the menu.
A solution to this is bpy.ops.script.reload() which in 2.7x has the shortcut F8 or can be run in the python console.  Recommend writing a little script to prepend it to the text editor and console headers for convenience
EDIT In 2.8 reload scripts is also  available via  the Blender &gt; System &gt; Reload Scripts menu.
Import the text script as a module
Further to this. To test a script in the python console.  For example the simple panel template.
This will give the script a &quot;namespace&quot;
&gt;&gt;&gt; foo = D.texts['ui_panel_simple.py'].as_module()

Can call the register method
&gt;&gt;&gt; foo.register()

and then later unregister with
&gt;&gt;&gt; foo.unregister()

somewhat more closely emulating it being an addon.