[Python-ideas] Desperate need for enhanced print function

M.-A. Lemburg mal at egenix.com
Mon Sep 7 11:01:12 CEST 2015


On 07.09.2015 01:48, Nick Coghlan wrote:
> On 6 September 2015 at 05:33, Anand Krishnakumar
> <anandkrishnakumar123 at gmail.com> wrote:
>> print("Hello, I am ", b, ". My favorite number is ", a, ".")
>>
>> I'm 14 and I came up with this idea after seeing my fellow classmates at
>> school struggling to do something like this with the standard print
>> statement.
>> Sure, you can use the format method but won't that be a bit too much for
>> beginners? (Also, casting is inevitable in every programmer's career)
> 
> Hi Anand,
> 
> Your feedback reflects a common point of view on the surprising
> difficulty of producing nicely formatted messages from Python code. As
> such, it currently appears likely that Python 3.6 will allow you and
> your peers to write output messages like this:
> 
>     print(f"Hello, I am {b}. My favorite number is {a}.")
> 
> as a simpler alternative to the current options:
> 
>     print("Hello, I am ", b, ". My favorite number is ", a, ".", sep="")
>     print("Hello, I am " + b + ". My favorite number is " + str(a) + ".")
>     print("Hello, I am {}. My favorite number is {}.".format(b, a))
>     print("Hello, I am {b}. My favorite number is {a}.".format_map(locals()))
>     print("Hello, I am %s. My favorite number is %s." % (b, a))

No need to wait for Python 3.6. Since print is a function, you
can easily override it using your own little helper to make
things easier for you. And this works in all Python versions starting
with Python 2.6:

"""
# For Python 2 you need to make print a function first:
from __future__ import print_function
import sys

_orig_print = print

# Use .format() as basis for print()
def fprint(template, *args, **kws):
    caller = sys._getframe(1)
    context = caller.f_locals
    _orig_print(template.format(**context), *args, **kws)

# Use C-style %-formatting as basis for print()
def printf(template, *args, **kws):
    caller = sys._getframe(1)
    context = caller.f_locals
    _orig_print(template % context, *args, **kws)

# Examples:
a = 1
fprint('a = {a}')
printf('a = %(a)s')

# Let's use fprint() as standard print() in this module:
print = fprint
b = 3
print('b = {b}')
"""

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Sep 07 2015)
>>> Python Projects, Coaching and Consulting ...  http://www.egenix.com/
>>> mxODBC Plone/Zope Database Adapter ...       http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________
2015-08-27: Released eGenix mx Base 3.2.9 ...     http://egenix.com/go83
2015-09-18: PyCon UK 2015 ...                              11 days to go

::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/


More information about the Python-ideas mailing list