Skip to content

Instantly share code, notes, and snippets.

@dannguyen
Created February 4, 2016 16:49
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dannguyen/b7d7ce593fe748157f34 to your computer and use it in GitHub Desktop.
Save dannguyen/b7d7ce593fe748157f34 to your computer and use it in GitHub Desktop.
the python print function
def print_(*args, **kwargs):
"""The new-style print function from py3k."""
fp = kwargs.pop("file", sys.stdout)
if fp is None:
return
def write(data):
if not isinstance(data, basestring):
data = str(data)
fp.write(data)
want_unicode = False
sep = kwargs.pop("sep", None)
if sep is not None:
if isinstance(sep, unicode):
want_unicode = True
elif not isinstance(sep, str):
raise TypeError("sep must be None or a string")
end = kwargs.pop("end", None)
if end is not None:
if isinstance(end, unicode):
want_unicode = True
elif not isinstance(end, str):
raise TypeError("end must be None or a string")
if kwargs:
raise TypeError("invalid keyword arguments to print()")
if not want_unicode:
for arg in args:
if isinstance(arg, unicode):
want_unicode = True
break
if want_unicode:
newline = u"\n"
space = u" "
else:
newline = "\n"
space = " "
if sep is None:
sep = space
if end is None:
end = newline
for i, arg in enumerate(args):
if i:
write(sep)
write(arg)
write(end)
@Aaron1233378
Copy link

There was a problem with this code.
Please redirect to Python Help to the very bottom.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment