git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Use Python's "print" as a function, not as a keyword

From
Frans Klaver <fransklaver@gmail.com>
Date
Dec 21, 2011, 08:43 UTC
Message-ID
<CAH6sp9M3eMFA5a1CSidO4WsSBVkvUQCAqh1VD1S11V2RRiYHKA@mail.gmail.com>
In-Reply-To
<20111221021930.GA31364@thinkpad>
On Wed, Dec 21, 2011 at 3:19 AM, Sebastian Morr <sebastian@morr.cc> wrote:
Show 15 quoted lines
> This has changed from Version 2.6 to Version 3.0. Replace all occurrences of
>
>    print ...
>
> with
>
>    print(...)
>
> and all occurrences of
>
>    print >> output, ...
>
> with
>
>    output.write(... + "\n")

While it's good to look forward, you shouldn't forget about testing on python 2.6. Lots of people still stick to that and maybe even to earlier versions.

Show 5 quoted lines
>  if len(argv) < 2:
> -       print 'Usage:', argv[0], '<zipfile>...'
> +       print('Usage:', argv[0], '<zipfile>...')
>        exit(1)
>

Here I would use the % notation: print('Usage: %s <zipfile>...' % argv[0])

Python 2.x would print a tuple:
>>> argv = ('import-zips.py',)
>>> print("Usage:", argv[0], '<zipfile>...')
('Usage:', 'import-zips.py', '<zipfile>...')
You could probably get away with
print('Usage: ' + argv[0] + ' <zipfile>...')

But that could probably become a readability issue. I would even wonder if it's considered pythonic.

It happens a few times more:
>  if verbose:
> -    print 'tip is', tip
> +    print('tip is', tip)
Show 27 quoted lines
> @@ -176,27 +176,27 @@ for cset in range(int(tip) + 1):
>     os.write(fdcomment, csetcomment)
>     os.close(fdcomment)
>
> -    print '-----------------------------------------'
> -    print 'cset:', cset
> -    print 'branch:', hgbranch[str(cset)]
> -    print 'user:', user
> -    print 'date:', date
> -    print 'comment:', csetcomment
> +    print('-----------------------------------------')
> +    print('cset:', cset)
> +    print('branch:', hgbranch[str(cset)])
> +    print('user:', user)
> +    print('date:', date)
> +    print('comment:', csetcomment)
>     if parent:
> -       print 'parent:', parent
> +       print('parent:', parent)
>     if mparent:
> -        print 'mparent:', mparent
> +        print('mparent:', mparent)
>     if tag:
> -        print 'tag:', tag
> -    print '-----------------------------------------'
> +        print('tag:', tag)
> +    print('-----------------------------------------')
Show 58 quoted lines
>
>     # checkout the parent if necessary
>     if cset != 0:
>         if hgbranch[str(cset)] == "branch-" + str(cset):
> -            print 'creating new branch', hgbranch[str(cset)]
> +            print('creating new branch', hgbranch[str(cset)])
>             os.system('git checkout -b %s %s' % (hgbranch[str(cset)], hgvers[parent]))
>         else:
> -            print 'checking out branch', hgbranch[str(cset)]
> +            print('checking out branch', hgbranch[str(cset)])
>             os.system('git checkout %s' % hgbranch[str(cset)])
>
>     # merge
> @@ -205,7 +205,7 @@ for cset in range(int(tip) + 1):
>             otherbranch = hgbranch[mparent]
>         else:
>             otherbranch = hgbranch[parent]
> -        print 'merging', otherbranch, 'into', hgbranch[str(cset)]
> +        print('merging', otherbranch, 'into', hgbranch[str(cset)])
>         os.system(getgitenv(user, date) + 'git merge --no-commit -s ours "" %s %s' % (hgbranch[str(cset)], otherbranch))
>
>     # remove everything except .git and .hg directories
> @@ -229,12 +229,12 @@ for cset in range(int(tip) + 1):
>
>     # delete branch if not used anymore...
>     if mparent and len(hgchildren[str(cset)]):
> -        print "Deleting unused branch:", otherbranch
> +        print("Deleting unused branch:", otherbranch)
>         os.system('git branch -d %s' % otherbranch)
>
>     # retrieve and record the version
>     vvv = os.popen('git show --quiet --pretty=format:%H').read()
> -    print 'record', cset, '->', vvv
> +    print('record', cset, '->', vvv)
>     hgvers[str(cset)] = vvv
>
>  if hgnewcsets >= opt_nrepack and opt_nrepack != -1:
> @@ -243,7 +243,7 @@ if hgnewcsets >= opt_nrepack and opt_nrepack != -1:
>  # write the state for incrementals
>  if state:
>     if verbose:
> -        print 'Writing state'
> +        print('Writing state')
>     f = open(state, 'w')
>     pickle.dump(hgvers, f)
>
> diff --git a/contrib/p4import/git-p4import.py b/contrib/p4import/git-p4import.py
> index b6e534b..144fafc 100644
> --- a/contrib/p4import/git-p4import.py
> +++ b/contrib/p4import/git-p4import.py
> @@ -26,11 +26,11 @@ if s != default_int_handler:
>  def die(msg, *args):
>     for a in args:
>         msg = "%s %s" % (msg, a)
> -    print "git-p4import fatal error:", msg
> +    print("git-p4import fatal error:", msg)
>     sys.exit(1)
>

I think that's it for the print(...,...) stuff. I might have misssed one or two though.

Show 29 quoted lines
> diff --git a/git-remote-testgit.py b/git-remote-testgit.py
> index 3dc4851..9803214 100644
> --- a/git-remote-testgit.py
> +++ b/git-remote-testgit.py
> @@ -81,9 +81,9 @@ def do_capabilities(repo, args):
>     """Prints the supported capabilities.
>     """
>
> -    print "import"
> -    print "export"
> -    print "refspec refs/heads/*:%s*" % repo.prefix
> +    print("import")
> +    print("export")
> +    print("refspec refs/heads/*:%s*" % repo.prefix)
>
>     dirname = repo.get_base_path(repo.gitdir)
>
> @@ -92,11 +92,11 @@ def do_capabilities(repo, args):
>
>     path = os.path.join(dirname, 'testgit.marks')
>
> -    print "*export-marks %s" % path
> +    print("*export-marks %s" % path)
>     if os.path.exists(path):
> -        print "*import-marks %s" % path
> +        print("*import-marks %s" % path)
>
> -    print # end capabilities
> +    print() # end capabilities
print("") here. 2.x:
>>> print()
()
Show 21 quoted lines
>
>
>  def do_list(repo, args):
> @@ -109,16 +109,16 @@ def do_list(repo, args):
>
>     for ref in repo.revs:
>         debug("? refs/heads/%s", ref)
> -        print "? refs/heads/%s" % ref
> +        print("? refs/heads/%s" % ref)
>
>     if repo.head:
>         debug("@refs/heads/%s HEAD" % repo.head)
> -        print "@refs/heads/%s HEAD" % repo.head
> +        print("@refs/heads/%s HEAD" % repo.head)
>     else:
>         debug("@refs/heads/master HEAD")
> -        print "@refs/heads/master HEAD"
> +        print("@refs/heads/master HEAD")
>
> -    print # end list
> +    print() # end list
print("")
Lots more to do, I'm afraid.

Cheers, Frans

Previous: Pete Wyckoff
Message 5 of 5 in “Use Python's "print" as a function, not as a keyword”
  1. Use Python's "print" as a function, not as a keywordSebastian Morr, Dec 21, 2011
  2. Ævar Arnfjörð BjarmasonDec 21, 2011
  3. Sverre RabbelierDec 21, 2011
  4. Pete WyckoffDec 27, 2011
  5. Frans KlaverDec 21, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.