2

Is there a way to globally set the sep parameter of the print function?
I'm getting tired of always doing this manually for every print statement.

Simple example:

some_superlong_list : List # existing list-object with A LOT of elements
print(*some_superlong_list, sep ="\n")

# do something with some_super_long_list

print("IDs:", *map(id.some_superlong_list), sep = "\n")
print("new values:", *some_super_long_list, sep = "\n")

Of course, I could add a shortcut sep = "\n", but I'd still have to assign the sep parameter for each print statement.

Is there a way to do something like this?

print.sep = "\n" # something like this, to set the 'sep' parameter to "\n" by default
Shlomo Gottlieb
  • 519
  • 5
  • 11
py_coffee
  • 85
  • 10
  • I also tried `print.__setattr__("sep","\n")`, which raised an AttributeError: `'builtin_function_or_method' object has no attribute 'sep'` – py_coffee Dec 10 '21 at 11:43

1 Answers1

2

Inspired by this answer:

from functools import partial

println = partial(print, sep='\n')

then you can use println whenever you want the line separation.

Shlomo Gottlieb
  • 519
  • 5
  • 11