Another way to go about this would be to skip the dependency of string naming and adopt retrieving nodes via UUID instead. See cmds.ls in conjunction with the -uuid flag found here.
Namespace are really just string name prefixes appended onto an object's name. As long as the namespace exists, you can rename an object to add/remove it from a namespace. For instance:
import maya.cmds as cmds
loc = cmds.spaceLocator()
cmds.ls(loc)[0] # Result: u'locator1' #
cmds.namespace(add='foobar') # Result: u'foobar' #
loc = cmds.rename(loc, ':foobar:{}'.format(loc)) # Result: u'foobar:my_locator' #
cmds.rename(loc, ':my_locator') # Result: u'my_locator' #
The idea behind UUID is that nodes get tagged with a special string attribute which is unique to the node and does not change when the node is renamed. For example, notice the name changes while the UUID remains the same:
import maya.cmds as cmds
loc = cmds.spaceLocator()
cmds.ls(loc, uuid=True)[0] # u'60446AC7-4398-E3CB-4C27-6BA417626E41' #
cmds.ls(loc)[0] # Result: u'locator1' #
loc = cmds.rename(loc, 'my_locator')
cmds.ls(loc, uuid=True)[0] # Result: u'60446AC7-4398-E3CB-4C27-6BA417626E41' #
cmds.ls(loc)[0] # Result: u'my_locator' #
Armed with this info, we now know that a node will always have the same, unchanging UUID for the entirety of its lifespan, regardless of what namespace it is moved (via naming) to/from.
Putting this all together, here is how we use the ls command in conjunction with uuid:
# Query the UUID of our locator
cmds.ls(loc, uuid=True)[0] # Result: u'60446AC7-4398-E3CB-4C27-6BA417626E41' #
# Retrieve the locator name string via UUID
cmds.ls(u'60446AC7-4398-E3CB-4C27-6BA417626E41')[0] # Result: u'my_locator' #