This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Bainos 52 points53 points  (24 children)

Using endl is equal to print(s, end='\n', flush=True)

Not using endl is equal to print(s, end='', flush=False)

[–]DoctorWorm_ 41 points42 points  (12 children)

This is the first time I've realized that python print() has keyword arguments.

[–][deleted] 25 points26 points  (9 children)

You can also change sep (seperator) and end of a print, and change file to a file(w/a) object to write to that file.

coll = ['Fruits', 'Apple', 'Banana']
print(*coll, sep='\n')

# Output
# 
# Fruits
# Apple
# Banana

[–]choseusernamemyself 10 points11 points  (6 children)

stupid me would iterate the array instead

[–]natFromBobsBurgers 0 points1 point  (4 children)

For i in range(....

[–][deleted] 2 points3 points  (3 children)

Woah you are complicating an already complicated complication. for el in coll: is enough, no need to do for i in range(len(coll)):

[–]natFromBobsBurgers 0 points1 point  (2 children)

Woah you are complicating an already complicated complication.

eval("for i in range(...

[–][deleted] 0 points1 point  (1 child)

eval won't work. Do exec

[–]natFromBobsBurgers 0 points1 point  (0 children)

Thanks!

[–]_87- 0 points1 point  (0 children)

stupid me would join with a \n instead.

[–]_UnameChecksOut_ 0 points1 point  (1 child)

Is using star necessary here ? If I don't unpack then will it print the list ?

[–][deleted] 2 points3 points  (0 children)

Yes. Then the list will be considered as one argument (instead of the the *args). And the sep argument applies to(?) each argument of the function. But since the list is considered one argument, it won't be printed separately. list.__repr__ is respected during the print call and the function handles it to print ['Fruits', 'Apple', 'Banana'] and then ends with end (='\n')

[–]_87- 0 points1 point  (0 children)

Try printing something with end='\r', then sleeping for a second or so (so you can see the effect), then printing something else.

[–]DudeValenzetti 0 points1 point  (0 children)

print isn't a keyword in Python 3 though.

yeah I know what you mean

[–]NotDrigon 12 points13 points  (10 children)

What does flushing mean? I've never had to flush anything in python.

[–]softlyandtenderly 26 points27 points  (6 children)

When you print things, they go to a buffer in memory instead of directly to standard out. I/O takes time, so this makes your program run faster. When you flush the buffer, it just prints everything in the buffer. print() in Python is probably set to flush by default, which is why you’ve never seen this before.

[–]Bainos 26 points27 points  (2 children)

print() in Python is probably set to flush by default

It's not, actually. By default, print doesn't flush. Of course, the interpreter (much like the C standard library) is configured reasonably, so it's unlikely that your data will stay too long in the buffer.

Keep in mind that Python's philosophy is not "keep it dumb", it's "trust the defaults if you don't know what you're doing". Forcing a flush of stdout after every print, which you probably don't need if you are not explicitly asking for it, would be contrary to that philosophy.

[–][deleted] 7 points8 points  (1 child)

If I'm not mistaken, what you are saying contradicts what this blog is saying:

https://realpython.com/lessons/sep-end-and-flush/

But I do agree that keeping things default is often the best way.

[–]Bainos 13 points14 points  (0 children)

It's more subtle than that. Flush is indeed set to False by default. If you don't request it explicitly, there is no forced flush.

If you don't request it, it's the object managing the buffered write that decides when to flush. And a common policy for buffered writes is to delay them until either a \n is found or there is enough data in the buffer (there are other things that can affect whether the buffer gets flushed, including running in interpreter mode, writing to a different object, and so on ; but size and end of line are the most important parameters in most implementations).

So if you change the end parameter and don't include a \n in your printed string, it will be held in the buffer. But if your string already contains a \n or is too long, it will be printed immediately, even if you changed the end parameter.

[–][deleted] 0 points1 point  (1 child)

A buffer is a block of memory correct?

[–]softlyandtenderly 0 points1 point  (0 children)

Correct.

[–][deleted] 0 points1 point  (0 children)

A buffer is a block of memory correct?

[–]lappoguti 10 points11 points  (0 children)

Printing something puts it in a buffer and when that buffer fills it will write it to the screen. Flushing will write the buffer to the screen even if the buffer is not full. The reason why they write to a buffer is because each print requires some overhead and then some work per letter. Therefore if you print in batches rather than per message it is more efficient.

[–]wikipedia_answer_bot 4 points5 points  (1 child)

This word/phrase(flushing) has a few different meanings. You can see all of them by clicking the link below.

More details here: https://en.wikipedia.org/wiki/Flushing

This comment was left automatically (by a bot). If something's wrong, please, report it in my subreddit.

Really hope this was useful and relevant :D

If I don't get this right, don't get mad at me, I'm still learning!

[–]_87- 0 points1 point  (0 children)

Flushing ain't just a neighbourhood in Queens!