diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 03c71a91..e04138fa 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -2982,6 +2982,8 @@ class Cmd(cmd.Cmd):
 
             if self.locals_in_py:
                 self.pystate['self'] = self
+            elif 'self' in self.pystate:
+                del self.pystate['self']
 
             localvars = self.pystate
             from code import InteractiveConsole

diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index a382a940..3d57a105 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -216,18 +216,35 @@ def test_base_shell(base_app, monkeypatch):
     assert m.called
 
 def test_base_py(base_app, capsys):
+    # Create a variable and make sure we can see it
     run_cmd(base_app, 'py qqq=3')
     out, err = capsys.readouterr()
     assert out == ''
-
     run_cmd(base_app, 'py print(qqq)')
     out, err = capsys.readouterr()
     assert out.rstrip() == '3'
 
+    # Add a more complex statement
     run_cmd(base_app, 'py print("spaces" + " in this " + "command")')
     out, err = capsys.readouterr()
     assert out.rstrip() == 'spaces in this command'
 
+    # Set locals_in_py to True and make sure we see self
+    out = run_cmd(base_app, 'set locals_in_py True')
+    assert 'now: True' in out
+
+    run_cmd(base_app, 'py print(self)')
+    out, err = capsys.readouterr()
+    assert 'cmd2.cmd2.Cmd object' in out
+
+    # Set locals_in_py to False and make sure we can't see self
+    out = run_cmd(base_app, 'set locals_in_py False')
+    assert 'now: False' in out
+
+    run_cmd(base_app, 'py print(self)')
+    out, err = capsys.readouterr()
+    assert "name 'self' is not defined" in err
+
 
 @pytest.mark.skipif(sys.platform == 'win32',
                     reason="Unit test doesn't work on win32, but feature does")

py command doesn't properly respect locals_in_py=False if it was ever True
Currently the **py** command will have ``self`` accessible forevermore if the ``locals_in_py`` attribute ever gets set to True - changing it back to False will have no effect.

Either setting it to False should make ``self`` no longer accessible within **py** OR ``locals_in_py`` should not be a runtime settable parameter.
tests/test_cmd2.py::test_base_py
tests/test_cmd2.py::test_version tests/test_cmd2.py::test_empty_statement tests/test_cmd2.py::test_base_help tests/test_cmd2.py::test_base_help_verbose tests/test_cmd2.py::test_base_help_history tests/test_cmd2.py::test_base_argparse_help tests/test_cmd2.py::test_base_invalid_option tests/test_cmd2.py::test_base_shortcuts tests/test_cmd2.py::test_base_show tests/test_cmd2.py::test_base_show_long tests/test_cmd2.py::test_base_show_readonly tests/test_cmd2.py::test_cast tests/test_cmd2.py::test_cast_problems tests/test_cmd2.py::test_base_set tests/test_cmd2.py::test_set_not_supported tests/test_cmd2.py::test_set_quiet tests/test_cmd2.py::test_set_onchange_hook tests/test_cmd2.py::test_base_shell tests/test_cmd2.py::test_base_run_python_script tests/test_cmd2.py::test_base_run_pyscript tests/test_cmd2.py::test_recursive_pyscript_not_allowed tests/test_cmd2.py::test_pyscript_with_nonexist_file tests/test_cmd2.py::test_pyscript_with_exception tests/test_cmd2.py::test_pyscript_requires_an_argument tests/test_cmd2.py::test_base_error tests/test_cmd2.py::test_history_span tests/test_cmd2.py::test_history_get tests/test_cmd2.py::test_base_history tests/test_cmd2.py::test_history_script_format tests/test_cmd2.py::test_history_with_string_argument tests/test_cmd2.py::test_history_with_integer_argument tests/test_cmd2.py::test_history_with_integer_span tests/test_cmd2.py::test_history_with_span_start tests/test_cmd2.py::test_history_with_span_end tests/test_cmd2.py::test_history_with_span_index_error tests/test_cmd2.py::test_history_output_file tests/test_cmd2.py::test_history_edit tests/test_cmd2.py::test_history_run_all_commands tests/test_cmd2.py::test_history_run_one_command tests/test_cmd2.py::test_history_clear tests/test_cmd2.py::test_base_load tests/test_cmd2.py::test_load_with_empty_args tests/test_cmd2.py::test_load_with_nonexistent_file tests/test_cmd2.py::test_load_with_directory tests/test_cmd2.py::test_load_with_empty_file tests/test_cmd2.py::test_load_with_binary_file tests/test_cmd2.py::test_load_with_utf8_file tests/test_cmd2.py::test_load_nested_loads tests/test_cmd2.py::test_base_runcmds_plus_hooks tests/test_cmd2.py::test_base_relative_load tests/test_cmd2.py::test_relative_load_requires_an_argument tests/test_cmd2.py::test_output_redirection tests/test_cmd2.py::test_output_redirection_to_nonexistent_directory tests/test_cmd2.py::test_output_redirection_to_too_long_filename tests/test_cmd2.py::test_feedback_to_output_true tests/test_cmd2.py::test_feedback_to_output_false tests/test_cmd2.py::test_allow_redirection tests/test_cmd2.py::test_pipe_to_shell tests/test_cmd2.py::test_pipe_to_shell_error tests/test_cmd2.py::test_base_timing tests/test_cmd2.py::test_base_debug tests/test_cmd2.py::test_base_colorize tests/test_cmd2.py::test_edit_no_editor tests/test_cmd2.py::test_edit_file tests/test_cmd2.py::test_edit_file_with_spaces tests/test_cmd2.py::test_edit_blank tests/test_cmd2.py::test_base_py_interactive tests/test_cmd2.py::test_exclude_from_history tests/test_cmd2.py::test_base_cmdloop_with_queue tests/test_cmd2.py::test_base_cmdloop_without_queue tests/test_cmd2.py::test_cmdloop_without_rawinput tests/test_cmd2.py::test_precmd_hook_success tests/test_cmd2.py::test_precmd_hook_failure tests/test_cmd2.py::test_interrupt_quit tests/test_cmd2.py::test_interrupt_noquit tests/test_cmd2.py::test_default_to_shell_unknown tests/test_cmd2.py::test_default_to_shell_good tests/test_cmd2.py::test_default_to_shell_failure tests/test_cmd2.py::test_ansi_prompt_not_esacped tests/test_cmd2.py::test_ansi_prompt_escaped tests/test_cmd2.py::test_custom_command_help tests/test_cmd2.py::test_custom_help_menu tests/test_cmd2.py::test_help_undocumented tests/test_cmd2.py::test_help_overridden_method tests/test_cmd2.py::test_help_cat_base tests/test_cmd2.py::test_help_cat_verbose tests/test_cmd2.py::test_select_options tests/test_cmd2.py::test_select_invalid_option tests/test_cmd2.py::test_select_list_of_strings tests/test_cmd2.py::test_select_list_of_tuples tests/test_cmd2.py::test_select_uneven_list_of_tuples tests/test_cmd2.py::test_help_with_no_docstring tests/test_cmd2.py::test_which_editor_good tests/test_cmd2.py::test_which_editor_bad tests/test_cmd2.py::test_multiline_complete_empty_statement_raises_exception tests/test_cmd2.py::test_multiline_complete_statement_without_terminator tests/test_cmd2.py::test_multiline_complete_statement_with_unclosed_quotes tests/test_cmd2.py::test_clipboard_failure tests/test_cmd2.py::test_commandresult_truthy tests/test_cmd2.py::test_commandresult_falsy tests/test_cmd2.py::test_is_text_file_bad_input tests/test_cmd2.py::test_eof tests/test_cmd2.py::test_eos tests/test_cmd2.py::test_echo tests/test_cmd2.py::test_pseudo_raw_input_tty_rawinput_true tests/test_cmd2.py::test_pseudo_raw_input_tty_rawinput_false tests/test_cmd2.py::test_pseudo_raw_input_piped_rawinput_true_echo_true tests/test_cmd2.py::test_pseudo_raw_input_piped_rawinput_true_echo_false tests/test_cmd2.py::test_pseudo_raw_input_piped_rawinput_false_echo_true tests/test_cmd2.py::test_pseudo_raw_input_piped_rawinput_false_echo_false tests/test_cmd2.py::test_raw_input tests/test_cmd2.py::test_stdin_input tests/test_cmd2.py::test_empty_stdin_input tests/test_cmd2.py::test_poutput_string tests/test_cmd2.py::test_poutput_zero tests/test_cmd2.py::test_poutput_empty_string tests/test_cmd2.py::test_poutput_none tests/test_cmd2.py::test_poutput_color_always tests/test_cmd2.py::test_poutput_color_never tests/test_cmd2.py::test_get_alias_names tests/test_cmd2.py::test_get_macro_names tests/test_cmd2.py::test_alias_no_subcommand tests/test_cmd2.py::test_alias_create tests/test_cmd2.py::test_alias_quoted_name tests/test_cmd2.py::test_alias_create_with_quoted_value tests/test_cmd2.py::test_alias_create_invalid_name[""] tests/test_cmd2.py::test_alias_create_invalid_name[!no_shortcut] tests/test_cmd2.py::test_alias_create_invalid_name[">"] tests/test_cmd2.py::test_alias_create_invalid_name["no>pe"] tests/test_cmd2.py::test_alias_create_invalid_name["no tests/test_cmd2.py::test_alias_create_invalid_name["nopipe|"] tests/test_cmd2.py::test_alias_create_invalid_name["noterm;"] tests/test_cmd2.py::test_alias_create_invalid_name[noembedded"quotes] tests/test_cmd2.py::test_alias_create_with_macro_name tests/test_cmd2.py::test_alias_list_invalid_alias tests/test_cmd2.py::test_alias_delete tests/test_cmd2.py::test_alias_delete_all tests/test_cmd2.py::test_alias_delete_non_existing tests/test_cmd2.py::test_alias_delete_no_name tests/test_cmd2.py::test_multiple_aliases tests/test_cmd2.py::test_macro_no_subcommand tests/test_cmd2.py::test_macro_create tests/test_cmd2.py::test_macro_create_quoted_name tests/test_cmd2.py::test_macro_create_with_quoted_value tests/test_cmd2.py::test_macro_create_invalid_name[""] tests/test_cmd2.py::test_macro_create_invalid_name[!no_shortcut] tests/test_cmd2.py::test_macro_create_invalid_name[">"] tests/test_cmd2.py::test_macro_create_invalid_name["no>pe"] tests/test_cmd2.py::test_macro_create_invalid_name["no tests/test_cmd2.py::test_macro_create_invalid_name["nopipe|"] tests/test_cmd2.py::test_macro_create_invalid_name["noterm;"] tests/test_cmd2.py::test_macro_create_invalid_name[noembedded"quotes] tests/test_cmd2.py::test_macro_create_with_alias_name tests/test_cmd2.py::test_macro_create_with_command_name tests/test_cmd2.py::test_macro_create_with_args tests/test_cmd2.py::test_macro_create_with_escaped_args tests/test_cmd2.py::test_macro_create_with_missing_arg_nums tests/test_cmd2.py::test_macro_create_with_invalid_arg_num tests/test_cmd2.py::test_macro_create_with_wrong_arg_count tests/test_cmd2.py::test_macro_create_with_unicode_numbered_arg tests/test_cmd2.py::test_macro_create_with_missing_unicode_arg_nums tests/test_cmd2.py::test_macro_list_invalid_macro tests/test_cmd2.py::test_macro_delete tests/test_cmd2.py::test_macro_delete_all tests/test_cmd2.py::test_macro_delete_non_existing tests/test_cmd2.py::test_macro_delete_no_name tests/test_cmd2.py::test_multiple_macros tests/test_cmd2.py::test_nonexistent_macro tests/test_cmd2.py::test_ppaged tests/test_cmd2.py::test_parseline_empty tests/test_cmd2.py::test_parseline tests/test_cmd2.py::test_readline_remove_history_item tests/test_cmd2.py::test_onecmd_raw_str_continue tests/test_cmd2.py::test_onecmd_raw_str_quit tests/test_cmd2.py::test_existing_history_file tests/test_cmd2.py::test_new_history_file tests/test_cmd2.py::test_bad_history_file_path tests/test_cmd2.py::test_get_all_commands tests/test_cmd2.py::test_get_help_topics tests/test_cmd2.py::test_exit_code_default tests/test_cmd2.py::test_exit_code_nonzero tests/test_cmd2.py::test_colors_default tests/test_cmd2.py::test_colors_pouterr_always_tty tests/test_cmd2.py::test_colors_pouterr_always_notty tests/test_cmd2.py::test_colors_terminal_tty tests/test_cmd2.py::test_colors_terminal_notty tests/test_cmd2.py::test_colors_never_tty tests/test_cmd2.py::test_colors_never_notty