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
__name__ variable to have a value “
If this file is
being imported from another module,
__name__ will be set to the
__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__ == "__main__":
which when run from text editor in above case registers the class
OBJECT_OT_add_object When run again it has "no idea" 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
__name__ == "my_addon") 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 > System > 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 "namespace"
>>> foo = D.texts['ui_panel_simple.py'].as_module()
Can call the register method
and then later unregister with
somewhat more closely emulating it being an addon.