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

1.627 visualizações
Pular para a primeira mensagem não lida

Andy Davidson

não lida,
7 de jun. de 2017, 18:07:4407/06/2017
para '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

não lida,
11 de jun. de 2017, 05:48:5711/06/2017
para 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”.
Responder a todos
Responder ao autor
Encaminhar
0 nova mensagem