FAQ
Quite often I type this


print('Total of accounts %.2f', total)


when I meant to type this


print('Total of accounts %.2f' % total)


Do I have to raise a PEP to get this stupid language changed so that it
dynamically recognises what I want it to do and acts accordingly?


Yours most frustratedly.


--
Python is the second best programming language in the world.
But the best has yet to be invented. Christian Tismer


Mark Lawrence

Search Discussions

  • Chris Angelico at Nov 1, 2013 at 1:53 am

    On Fri, Nov 1, 2013 at 12:45 PM, Mark Lawrence wrote:
    Quite often I type this

    print('Total of accounts %.2f', total)

    when I meant to type this

    print('Total of accounts %.2f' % total)

    Do I have to raise a PEP to get this stupid language changed so that it
    dynamically recognises what I want it to do and acts accordingly?

    I feel your frustration there, especially since the usage you're
    attempting parallels C's printf function. But the parsing would be
    ambiguous - is it that any percent sign changes the meaning of print's
    arguments? Or is this some kind of DWIM system, where the meaning
    depends on whether the arguments "would work" if you used a percent
    sign?


    What you _could_ do, though, is decide that you will never EVER use
    print with multiple arguments. Then just do this:


    orig_print = print
    def print(fmt, *args, **kwargs):
         orig_print(fmt%args, **kwargs)


    Now your print function takes printf-style arguments. Note that this
    WILL error out if you have percent signs and no arguments; you may
    want to use "if args" to guard against that, or else just accept that
    it'll break if you do stuff that would be broken with printf.


    (You may want to play with functools.wraps for the benefit of
    docstrings and stuff... or maybe you don't care.)


    One of the advantages of print being a function is that you can
    actually do this :) With print as a statement, that option isn't
    there.


    ChrisA
  • Chris Angelico at Nov 1, 2013 at 1:54 am

    On Fri, Nov 1, 2013 at 12:53 PM, Chris Angelico wrote:
    orig_print = print
    def print(fmt, *args, **kwargs):
    orig_print(fmt%args, **kwargs)

    PS. To be courteous to subsequent developers, you might want to
    instead leave print as it is, and create your own printf:


    def printf(fmt, *args, **kwargs):
         print(fmt%args, **kwargs)


    Take your pick, whichever way works for you.


    ChrisA
  • Peter Cacioppi at Nov 1, 2013 at 7:11 am
    Mark said :


    "Do I have to raise a PEP to get this stupid language changed so that it
    dynamically recognises what I want it to do and acts accordingly?"


    The printf syntax in C isn't any wonderful thing, and there is no obligation to provide some Python version of it.


    I have to say, there were a few things that twerked me when getting up to speed on Py, but formatting strings wasn't one of them. I'd be surprised if Guido prioritizes a fix for this part of his "stupid language".


    Just sayin
  • Chris Angelico at Nov 1, 2013 at 7:59 am

    On Fri, Nov 1, 2013 at 6:11 PM, Peter Cacioppi wrote:
    The printf syntax in C isn't any wonderful thing, and there is no obligation to provide some Python version of it.

    Maybe, but it's supported by so many languages that it is of value.
    Though Python's use of the % operator does lead to edge cases (a
    single argument, except if it's a tuple, even though a tuple could be
    accepted by %r), so a printf() function might be cleaner.


    ChrisA
  • Mark Lawrence at Nov 1, 2013 at 9:08 am

    On 01/11/2013 07:11, Peter Cacioppi wrote:
    Mark said :

    "Do I have to raise a PEP to get this stupid language changed so that it
    dynamically recognises what I want it to do and acts accordingly?"

    The printf syntax in C isn't any wonderful thing, and there is no obligation to provide some Python version of it.

    I have to say, there were a few things that twerked me when getting up to speed on Py, but formatting strings wasn't one of them. I'd be surprised if Guido prioritizes a fix for this part of his "stupid language".

    Just sayin

    I'm heading into town in maybe an hour. I'll stop here
    http://www.toolbankexpress.com/shop/castle/ so I can get an extremely
    large pair of pliers with which I can extract my tongue from my cheek :)


    --
    Python is the second best programming language in the world.
    But the best has yet to be invented. Christian Tismer


    Mark Lawrence
  • Chris Angelico at Nov 1, 2013 at 9:17 am

    On Fri, Nov 1, 2013 at 8:08 PM, Mark Lawrence wrote:
    I'm heading into town in maybe an hour. I'll stop here
    http://www.toolbankexpress.com/shop/castle/ so I can get an extremely large
    pair of pliers with which I can extract my tongue from my cheek :)

    I think you may have a bit of trouble. The part where you demanded the
    language dynamically figure out what you wanted may have gotten your
    tongue rather stuck there...


    The neat thing about this list is that even posts like that can result
    in real solutions :)


    ChrisA
  • Mark Lawrence at Nov 1, 2013 at 9:32 am

    On 01/11/2013 09:17, Chris Angelico wrote:
    On Fri, Nov 1, 2013 at 8:08 PM, Mark Lawrence wrote:
    I'm heading into town in maybe an hour. I'll stop here
    http://www.toolbankexpress.com/shop/castle/ so I can get an extremely large
    pair of pliers with which I can extract my tongue from my cheek :)
    I think you may have a bit of trouble. The part where you demanded the
    language dynamically figure out what you wanted may have gotten your
    tongue rather stuck there...

    The neat thing about this list is that even posts like that can result
    in real solutions :)

    ChrisA

    Absolutely correct.


    This thread actually got me thinking[1]. There's a thread on Python
    ideas about "where did we go wrong with negative stride?". It discusses
    maybe fixing this for Python 4. I wondered if the idea of deprecating
    printf style formatting might also come up again. It was discussed in
    some depth here
    https://mail.python.org/pipermail/python-dev/2012-February/116789.html


    [1] yes it does happen, yes you may joke about it and yes I can take the
    jokes, I certainly deserve them :)


    --
    Python is the second best programming language in the world.
    But the best has yet to be invented. Christian Tismer


    Mark Lawrence
  • Chris Angelico at Nov 1, 2013 at 9:42 am

    On Fri, Nov 1, 2013 at 8:32 PM, Mark Lawrence wrote:
    I wondered if the idea of deprecating printf style formatting might also
    come up again. It was discussed in some depth here
    https://mail.python.org/pipermail/python-dev/2012-February/116789.html

    I don't see any reason for it to go. Anyone who wants to use .format()
    is welcome to; anyone who likes %s is welcome to use that. They have
    sufficiently distinct styles that they don't really come into
    conflict. Now, if someone wants to deprecate the "str modulo anything"
    operator in favour of an explicit sprintf() function (optionally with
    printf() that sends straight through to print()), that I might get
    behind - but I don't see any reason to lose the printf-style
    formatting codes.


    ChrisA
  • Peter Cacioppi at Nov 1, 2013 at 9:47 pm
    Mark said :


    "so I can get an extremely large pair of pliers with which I can extract my tongue from my cheek :) "


    OK fair enough, and my post was in the same spirit.


    Chris said :


    "Maybe, but it's supported by so many languages that it is of value. "


    Sure, I suppose someone should make a module that gets you as close as possible within the as-is language definition. I can't really comment on whether Py is close enough currently, as I always thought printf was sort of crappy.


    I thought the C++ << business even worse.


    Like I said, Python seems fine to me in this area. I use the str(), round() and ljust(), rjust() functions and concatenate what I need together. The result seems more readable than the old printf crap I used to write (not exactly clearing a sky high bar).


    Just one man-child's opinion.
  • Chris Angelico at Nov 1, 2013 at 11:13 pm

    On Sat, Nov 2, 2013 at 8:47 AM, Peter Cacioppi wrote:
    I always thought printf was sort of crappy.

    I thought the C++ << business even worse.

    Oh, you'll get no argument from me about the std::*stream types! When
    I write C++ code, I almost exclusively use C-style formatted strings,
    even sometimes going to the extent of using fopen() just so I can use
    fprintf() rather than fstream. Also, I often create a class something
    like this:


    struct format
    {
         char *data;
         format(const char *fmt, ...);
         operator char *() {return data;}
    };


    where the constructor calls vsprintf and there's a bit of new/delete
    work to manage memory, but that's all under the covers; in code, I use
    it like this:


    some_function_call(format("Hello, %s!", "world"));


    making it almost as if it's a function returning a string, like you
    would in Python.


    Most of the problems with printf come from the fragility of C's
    variadic functions. Some C compilers can deal with that (gcc happily
    checks printf args), but there are still fundamentally hard problems
    around it. They don't apply in Python (or Pike, which has an sprintf
    function[1] with even more power), as issues can be signalled with
    exceptions - clean, easy, safe.


    ChrisA


    [1] http://pike.lysator.liu.se/generated/manual/modref/ex/predef_3A_3A/sprintf.html
  • Peter Cacioppi at Nov 2, 2013 at 1:11 am
    Chris said :


    " I almost exclusively use C-style formatted strings, even sometimes going to the extent of using fopen() just so I can use fprintf() rather than fstream. Also, I often create a class something like this: "


    Ditto all that, to include the special class I cooked up to handle printf issues in an object based way.


    In general, I liked all the C++ additions a lot. I really liked the STL. But I don't know what Bjarne was thinking with the streams.
  • Chris Angelico at Nov 2, 2013 at 1:18 am

    On Sat, Nov 2, 2013 at 12:11 PM, Peter Cacioppi wrote:
    In general, I liked all the C++ additions a lot. I really liked the STL. But I don't know what Bjarne was thinking with the streams.

    It does look cool on paper. You can "see" the flow of data.
    Everything's type-safe - unlike C's scanf family (though, again,
    sscanf in a high level language can get around that problem). The only
    problem is, it doesn't actually help much in code. It's snazzy but not
    all that useful.


    ChrisA
  • Peter Cacioppi at Nov 2, 2013 at 1:26 am
    Chris said :


    "It does look cool on paper. "


    Sure, I'll buy that Bjarne designed something intellectually pretty that just doesn't flow very well in practice. Most of his C++ worked very well both in theory and practice, so I can forgive him one nerdy overreach, if that's what it was.


    Python is impressive for it's lack of such constructions. Things are pretty to think about, and handy to use.


    What is it the French say? "Sure it works in practice, but does it work on paper?" ;) I can joke, I once had many very smart French colleagues.
  • MRAB at Nov 1, 2013 at 2:01 am

    On 01/11/2013 01:45, Mark Lawrence wrote:
    Quite often I type this

    print('Total of accounts %.2f', total)

    when I meant to type this

    print('Total of accounts %.2f' % total)

    Do I have to raise a PEP to get this stupid language changed so that it
    dynamically recognises what I want it to do and acts accordingly?

    Yours most frustratedly.
    Could something like pylint recognise a format string literal as the
    first argument of 'print' and warn if it's followed by a comma?
  • Michael Torrie at Nov 1, 2013 at 2:41 am

    On 10/31/2013 07:45 PM, Mark Lawrence wrote:
    Quite often I type this

    print('Total of accounts %.2f', total)

    when I meant to type this

    print('Total of accounts %.2f' % total)

    Do I have to raise a PEP to get this stupid language changed so that it
    dynamically recognises what I want it to do and acts accordingly?

    Yours most frustratedly.

    This is one reason I've started using the new string formatting method
    in all my new code.
  • Mark Lawrence at Nov 1, 2013 at 2:56 am

    On 01/11/2013 02:41, Michael Torrie wrote:
    On 10/31/2013 07:45 PM, Mark Lawrence wrote:
    Quite often I type this

    print('Total of accounts %.2f', total)

    when I meant to type this

    print('Total of accounts %.2f' % total)

    Do I have to raise a PEP to get this stupid language changed so that it
    dynamically recognises what I want it to do and acts accordingly?

    Yours most frustratedly.
    This is one reason I've started using the new string formatting method
    in all my new code.

    I'll admit to being a lazy, bone idle git and not wanting to change my
    code as a former C programmer, but that strikes me as a cunning plan,
    thanks. Now where's my Windows desktop shortcut to the Python compiled
    help file? :)


    --
    Python is the second best programming language in the world.
    But the best has yet to be invented. Christian Tismer


    Mark Lawrence
  • Michael Torrie at Nov 1, 2013 at 4:40 am

    On 10/31/2013 08:56 PM, Mark Lawrence wrote:
    On 01/11/2013 02:41, Michael Torrie wrote:
    On 10/31/2013 07:45 PM, Mark Lawrence wrote:
    Quite often I type this

    print('Total of accounts %.2f', total)

    when I meant to type this

    print('Total of accounts %.2f' % total)

    Do I have to raise a PEP to get this stupid language changed so that it
    dynamically recognises what I want it to do and acts accordingly?

    Yours most frustratedly.
    This is one reason I've started using the new string formatting method
    in all my new code.
    I'll admit to being a lazy, bone idle git and not wanting to change my
    code as a former C programmer, but that strikes me as a cunning plan,
    thanks. Now where's my Windows desktop shortcut to the Python compiled
    help file? :)

    I only know enough to do the basic stuff that works for me 3/4 of the
    time.


    "{} {} {}".format(one, two, three)


    Anything more than that I have to look up! Sometimes the ability to use
    keywords and dictionaries in variable placement is kind of nice.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedNov 1, '13 at 1:45a
activeNov 2, '13 at 1:26a
posts18
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase