0

Running the following codes

with open('abc', 'wt') as fh:
    fh.write('\n'.join(['a','b','c']))

in ipython, no outputs as expected; however, in jupyter notebook, output like [Out 4]: 5.

It's possibly related to

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

in jupyter. But what's the strange thing jupyter output and how to prevent?

0

1 Answer 1

1

I'm not familiar with jupyter, but if it's a REPL that prints the results of expressions as they're evaluated, it's printing the return value of fh.write. write returns the numbers of characters written, so that's being printed out.

_ = fh.write prevents the output because = is a statement that does not evaluate to a value. You may also be able to prevent the output by doing something like fh.write(...); None. I wouldn't recommend doing this in real code, but for the sake of limiting output, it may be beneficial.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.