winpdb is not recognising function inputs? - python

This is my first post! :D
I am trying to learn how to use winpdb to debug some python code and have a problem. Consider the following python function, simple.py:
def simple(a,b):
c = a + b
return c
I am in windows and using the command line in the directory where I have stored this function I attempt to run winpdb with the following command:
winpdb simple.py 2 1
Is this the correct way to debug the function simple.py with a = 2 and b = 1? As when I execute the above in the command line winpdb launches but with a and b undefined, for example (taken from the winpdb console when the above is entered into the cmd window):
> eval a
<type 'exceptions.NameError'>, name 'a' is not defined
I am sorry to have to ask such a basic question, but I can not seem to find any solutions online.

The presented source file defines a function but it never calls the function and doesn't execute any code at all. Code in exactly that form can't be trivially debugged.
Typically a sample call to function is added to the end of file, like
def simple(a, b):
c = a + b
return c
simple(1, 2)
Than you can start winpdb like
winpdb simple.py
place breakpoint in a function by clicking on margin of c = a + b line and press the Go button.
After that the program would stop in a state where you can use eval a and even eval simple(5, 6)

Related

Python Return Command In Recursion Function

While learning Python and browsing the internet I stumble upon a piece of code in w3schools.com. I tried to run it using their built-in site Python IDLE and using my own Python 3.9.0 Shell. What I got is two different outputs.
I want to know which output is the correct output and why is it providing two different outputs.
The Codes and Its Output
Built in site Python IDLE
Python 3.9.0 Shell
Notice that the number 21 is only printed(outputted) once when running the code using Built-in site Python IDLE, while it is printed(outputted) twice when running the code using Python 3.9.0 Shell.
My Own Debugging
I have tried a simple print statement debugging. Checking the result there is only one different, using Python 3.9.0 Shell the last return line is executed and it outputted the last result while using the Built-in site Python IDLE the last return is either not executed or not outputted, in this case, I believe it is the former, and I believe the correct output is the Python 3.9.0 Shell, but I have no idea why are there two different output.
Print Statement Using Python 3.9.0 Shell Result Part 1 Result Part 2
Print Statement Using Built-in site Python IDLE Result Part 1 Result Part 2
Source Code
def tri_recursion(k):
if(k>0):
result = k + tri_recursion(k-1)
print(result)
else:
result = 0
return result
tri_recursion(6)
You have added a return result statement at the end. For any IDE, unless you print that value, it wouldn't be displayed. However, IDLE prints the return value as well. Technically, both outputs are correct, since both interpreters are configured to do different actions. As a small example,
def foo():
return(1)
Running foo() on IDLE gives >>> 1, whereas it gives nothing on other IDE's, as there is no print statement
Both are correct.
The built-in python on the site is showing you the output of the execution of your program. This is equivalent to running the following program:
if __name__ == '__main__':
tri_recursion(6)
add this at the end of your code, save it as test.py and run it like this:
python test.py
the results will be the same
The python shell is showing you the output of the REPL (Read-Eval-Print-Loop), the print statement will print to the screen, but the return value of the function is also printed, because of the REPL, there's no way to avoid this, it is so by design.
You could design your function to not return anything, but it wouldn't be recursive anymore.
both are correct you need to understand that the python shell is printing statement's output.
when you write :
>>> tri_recursion(6)
it will execute first all the print inside function then it prints value that the last call returns.

How to edit a previously defined function in ipython without scrolling all the way back

If I'm using ipython and define my own function, say
def f(x):
return x + 1
and later decide that I would like to edit this function to
def f(x,m,c):
return m*x + c
is there a quick way to access the function code without scrolling back with the arrows?
As #RuthC has pointed out, within IPython there is a pretty cool way to edit a previously defined function. If you partially type in the first part of your def, e.g. def, def myf, or def myfunc, you can then press Ctl+P to scroll through various defs in your current session. The really cool thing about this mode is that the entire function definition can be retrieved and you can use up and down arrow keys to edit specific lines of your function definition.
For longer functions, you might prefer to edit a .py script in your favorite editor and use the %run magic command within iPython to rerun it
in myscript.py
def f(x):
return x + 1
in iPython:
In [1] %run myscript.py
In [2] f(1)
Out[2] 2
Oh wait. I want to make a change (and save!) myscript.py
def f(x,m,c):
return m*x + c
Back to iPython
In [3] %run myscript.py
In [4] f(1,2,3)
Out[4] 5

Is it possible to run a python file from the command line, but interact with it? (REPL)

Suppose I have a very simple test.py, and here is the code:
def sum(a, b):
return a + b
print(sum(5, 6))
In my cmd, when I do python test.py, the command lines returns 11.
What I would like to do is interact with it, so I want to type, sum(4,2) and the command line should return 6, but instead I get:
sum is not recognized as an internal or external command ...
Basically I want to have the REPL feature, like WingIDE, Sublime Text. Is this possible from the command line?
For this purpose, you'd better pass input arguments when you run your program in cmd.
I put the code with minor modifications as below,
def sum(a, b):
return a+b
import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
print(sum(a, b))
Then in cmd, you just need to run like this:
>> python test.py 4 2

Simulate shell behavior (force eval of last command to be displayed)

In a python shell, if I type a = 2 nothing is printed. If I type a 2 gets printed automatically. Whereas, this doesn't happen if I run a script from idle.
I'd like to emulate this shell-like behavior using the python C api, how is it done?
For instance, executing this code PyRun_String("a=2 \na", Py_file_input, dic, dic); from C, will not print anything as the output.
I'd like to simulate a shell-like behavior so that when I execute the previous command, the value "2" is stored in a string. Is it possible to do this easily, either via python commands or from the C api? Basically, how does the python shell do it?
To compile your code so expression statements invoke sys.displayhook, you need to pass Py_single_input as the start parameter, and you need to provide one statement at a time.

Sublime tries to compile Python-script as a Haskell-script [duplicate]

This question already has answers here:
How do I run Python code from Sublime Text 2?
(16 answers)
Closed 6 years ago.
I am newbie, pls don't be to harsh with me.
I am trying to setup Sublime Text with Python (for next Semester). Before that I used Haskell in SublimeText, where I could run my skript with "ctrl+b" in Sublime.
When I try doing the same with a file named "test.py".
def add(a,b):
return a+b
main = print(add(2,3))
I get the error-message:
/home/nayooti/Desktop/test.py:1:1:
**Parse error: naked expression at top level**
[Finished in 0.2s with exit code 1]
[shell_cmd: runhaskell "/home/nayooti/Desktop/test.py"]
[dir: /home/nayooti/Desktop]
[path: /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games]
The "naked expression at top level"-part looks familiar, since it is very Haskell specific. Indeed, when I search the Web for exactly this message, it brings me to Haskell related stuff only. So apparently Sublime or the compiler thinks, I am trying to run a Haskell-script, even though I named the file ~.py .
For these, who are not familiar with Haskell and Python:
you can usually run a script by:
Python: main = print(method(x,y))
Haskell: main = print(function x y)
I am using Ubuntu 12.04. What am I doing wrong here? Help is very appreciated. Tx.
Go to Tools -> Build System and make sure that Python is selected. Also, that is not how you write main in Python, it should be more like
def add(a, b):
return a + b
def main():
print(add(2, 3))
if __name__ == '__main__':
main()
When you write
main = print(add(2, 3))
And load your script, that line gets executed since it is an assignment, much the same if I did
a = 1
Or
a = print(add(2, 3))
There is nothing special about the name main in Python. You still see your output because print has side effects, but rather than defining main you are just executing a script. The difference comes in if you were to try
add.py:
def add(a, b):
return a + b
main = print(add(2, 3))
subtract.py:
from add import add
def subtract(a, b):
return add(a, -b)
If you ran python subtract.py, you would still see 5 printed to the screen, even though there is no main defined in subtract.py. Obviously, this is not desired behavior, your main function in add.py should not be executing if you did not run it as the main script. When you run a Python script directly as python scriptname.py, there is a variable set global to that file called __name__ that gets assigned the string "__main__". If you import it, then __name__ is set to its qualified module name, something like "scriptname". This gives you the ability to define different behavior if the file is executed compared to if the file is imported by another script.

Categories