Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

python - IPython magic print variables on assignment

Is there a parameter in IPython to print the variables each time I assign them ?

Currently:

In [1]: a = 5

In [2]: a
Out[2]: 5

In [3]: b = a + 1

In [4]: b
Out[4]: 6

In matlab, the variable is printed unless one writes a ; at the end, in ipython I would like to activate this with a magic function like so:

In [1]: %printassignment

In [2]: a = 5
a = 5

In [3]: b = a + 1
b = 6

I could write my own magic, but I'm pretty sure that's something that is so standard people would craft a super tuned, standard, function. The feature is especially useful for beginners.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As of IPython 6.2, this exists, but the command to turn it on is really long:

%config InteractiveShell.ast_node_interactivity='last_expr_or_assign'

You can set it in your ipython_config.py, if you want it to be on by default:

c = get_config()
c.InteractiveShell.ast_node_interactivity = 'last_expr_or_assign'

It only triggers when the assignment target is a simple name; assignments like arr[i] = val or obj.attr = 3 will not auto-print.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...