Python: Enter or leave "User Perspective (local)"
Check the menu in the 3D view, and right-click the "View Global/Local" menu item. You can then choose "Copy Python Command". It'll give you bpy.ops.view3d.localview(), which is the operator that is executed when you click on that menu item.

Note that it does require a context of a 3D view, so if you try to execute that command as-is in the Python console, it'll refuse (your Python console doesn't have Global/Local view). However, if you execute it as part of your own operator running in a 3D view, it'll be fine. In that case you can also simply use context.area.spaces.active.local_view instead of iterating over all areas in the current screen.