newbie: is there an easy way to scope variables to a cell?

1,641 views
Skip to first unread message

Andy Davidson

unread,
Jun 7, 2017, 6:07:44 PM6/7/17
to 'Ryan Blue' via Project Jupyter
I am an experienced programmer and know many languages. I am fairly new to python and notebooks

I find my self spending a lot of time debugging my notebooks. What I have noticed is that in a given cell I typically define a function and some additional test code. Often the test code defines some variables. The bug tends to be latter I define a new function f2() that accidentally picks up one of the test variables I previously defined for f1(). So I incorrectly think f2() works. Latter in f3() I wast a ton of time because my assumption that f2() works was never true. What I really wanted to do was pass the accidental global as a function argument

I guess I could write my code as follows to ensure test variables do not escape to the global space

Def f1(a, b):

Def f1Test():
A = 1
B = 2
Assert f1(a,b) == xyzzy

ftTest()

Is there a stylistically more pleasing way to avoid these kinds of errors?

Is there some sort of “lint” for notebooks?

Another common bug I typically have are typos in functions. I.E. I miss spell a variable. I like the notebook REPL environment how ever writing lots of test code is a big productivity hit


Kind regards

Andy

Lawrence D’Oliveiro

unread,
Jun 11, 2017, 5:48:57 AM6/11/17
to Project Jupyter
On Thursday, June 8, 2017 at 10:07:44 AM UTC+12, Andy Davidson wrote:
What I have noticed is that in a given cell I typically define a function and some additional test code. Often the test code defines some variables. The bug tends to be latter I define a new function f2() that accidentally picks up one of the test variables I previously defined for f1(). So I incorrectly think f2() works.

That’s not a bug, that’s a feature. I find a good way to ensure that a notebook makes sense without any accumulated unwanted context is to use the “Kernel → Restart & Clear Output” option, followed by “Cell → Run All”.
Reply all
Reply to author
Forward
0 new messages