I don't know if this is the best solution, but it works.
(defun evil*-complete-previous () (interactive)
(let ((old (string (char-syntax ?_))))
(modify-syntax-entry ?_ "w")
(modify-syntax-entry ?_ old)))
(define-key evil-insert-state-map (kbd "M-p") 'evil*-complete-previous)
Use C-u C-c C-c.
See python-shell-buffer-substring function docstring:
When optional argument NOMAIN is non-nil everything under an if __name__ == '__main__' block will be removed.
When optional argument SEND-MAIN is non-nil, allow execution of
code inside blocks delimited by if __name__== '__main__':.
When called ...
Generally forward-sexp should know what to do in any given mode, so you could perhaps base it on this:
(save-excursion (ignore-errors (forward-sexp) (1- (point))))
Something like how show-paren-mode works
You could use that exact same code. Look at M-x find-function RET show-paren-function and how it uses the results of (funcall show-...
Just use forward-sexp instead (bound to C-M-f). Likewise, backward-sexp (C-M-b).
See (elisp) List Motion.
If you don't want to do that then you'll need to change the syntax for character * in your buffer to be word syntax:
(modify-syntax-entry ?* "w")
Or for a given syntax table, my-table:
(modify-syntax-entry ?* "w" my-table)