Does the print statement need to be indented?

Does it matter if I indent the print statement? It seems like I get the same thing whether I indent it or not.

My solution was this one


import json
with open("message.json") as message_json:
  pass
  message=json.load(message_json)
print(message['text'])

looks like python needs the variable to be indented under the Pass block in order to save the contento of the json

Print doesn’t need to be indented, as shown here.

However, pass is just a null statement. You use it to test your code when you’re using with before writing anything after it so you don’t get an end of frame error. Once you have the message portion, you can delete pass.

7 Likes

To my suprise, doesn’t seem to matter. The file oject variable seems to be global in nature.

5 Likes

If I understand, the end of the “with open…” indented section simply tells Python when to close the file we’re access because we don’t need it any more.

with open('message.json') as message_json:
    message = json.load(message_json)
    print(message)

In this context, if the print function in indented, the text will be printed and then the file will be closed.

with open('message.json') as message_json:
    message = json.load(message_json)

print(message)

If the print function isn’t indented, the file will be closed and then the text will be printed.

They both work, but that’s the difference.

3 Likes

The way I understand it, we can’t perform any more operations on message.json once the file is closed. So only indented lines work in that context.

However the ‘message’ object we created is now held in Python’s memory, allowing us to print it even when the original file is closed.

This could be incorrect but I think it’s why the print works either way.

2 Likes

I’m also a little confused here. I have some trouble with what is remaining in Python’s memory sometimes.

With functions (e.g. def function_name() ), any variables created within it are temporary, and are “forgotten” once the function ends. And even though it returns a variable sometimes ( e.g. return variable_name ), it is returning the result of that variable from the function, NOT the variable itself for us to use in any way. I feel like I get that now

However, with this with and open() line ( e.g. with open(‘message.json’) as message_json: ), we’re temporarily opening a file, using information from it to store as variables while it’s open (aka when things are indented after it), then closing it. But in this case, unlike with functions, these are permanent variables we’re creating?

1 Like

I’m wondering the same - maybe this applies only from JSON files?

I’m guessing that any time we open a file that way (JSON, and other files) and store something as a variable, it “permanently” keeps it for us, even after the file is closed. That would be very handy, so I assume that’s the way it’s made to work. Lol

Although it’s indented and part of its own code block the body of a with statement doesn’t have its own scope (as functions and classes would) so names you assign there are available to the surrounding scope/namespace.

This behaviour is consistent with other indented code such as if statements and for loops (with classes and functions being the exception)-

for x in [1, 2, 3]:
    b = x
    continue  # do nothing else in loop, just go to next iteration

print(b)
Out: 3
print(x)  # x is also still assigned (unlike some languages)
Out: 3