[Tutor] Tutor Digest, Vol 143, Issue 4

yehudak . katye2007 at gmail.com
Sun Jan 3 09:35:24 EST 2016


Thank you ALL for your kind help.

Yehuda

On Sun, Jan 3, 2016 at 3:12 PM, <tutor-request at python.org> wrote:

> Send Tutor mailing list submissions to
>         tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
>         tutor-request at python.org
>
> You can reach the person managing the list at
>         tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>    1. To FORMAT or not to (yehudak .)
>    2. Re: To FORMAT or not to (Chris Warrick)
>    3. Re: To FORMAT or not to (Francois Dion)
>    4. Re: To FORMAT or not to (Peter Otten)
>    5. Re: To FORMAT or not to (Francois Dion)
>    6. Re: To FORMAT or not to (Alan Gauld)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Sun, 3 Jan 2016 14:27:01 +0200
> From: "yehudak ." <katye2007 at gmail.com>
> To: tutor at python.org
> Subject: [Tutor] To FORMAT or not to
> Message-ID:
>         <CAE3ie40p8HDmcMNBQxYf9GSurTPkzWP+jF6=H+Jpxig=
> Se6sLQ at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> Hi there,
> In a program I wrote the following line (Python 3.5):
>
> print("You've visited", island, '&', new + ".")
>
> A programmer told me that it's a bad habit, and I should have used instead:
>
> print("You've visited {0} {1} {2}{3}".format(island, "&", new, "."))
>
> May I understand why?
>
>
> ------------------------------
>
> Message: 2
> Date: Sun, 3 Jan 2016 14:04:22 +0100
> From: Chris Warrick <kwpolska at gmail.com>
> To: "yehudak ." <katye2007 at gmail.com>
> Cc: tutor at python.org
> Subject: Re: [Tutor] To FORMAT or not to
> Message-ID:
>         <CAMw+j7+RTGqSFOQg=
> Fohx0xohV8ZFc3JJSn3GES2eMz-_TRCPw at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> On 3 January 2016 at 13:27, yehudak . <katye2007 at gmail.com> wrote:
> > Hi there,
> > In a program I wrote the following line (Python 3.5):
> >
> > print("You've visited", island, '&', new + ".")
> >
> > A programmer told me that it's a bad habit, and I should have used
> instead:
> >
> > print("You've visited {0} {1} {2}{3}".format(island, "&", new, "."))
> >
> > May I understand why?
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
>
> The programmer was not very intelligent in his use of str.format in
> the first place. A more sensible way to write this is:
>
> print("You've visited {0} & {1}.".format(island, new))
>
> Formatting with constant strings is pointless, just include it in the
> original input. However, string formatting is not.
>
> Here are a couple of reasons:
> * String formatting works everywhere, but this syntax is specific to
> print() ? if you use something else, you might end up producing faulty
> code
> * The corrected string formatting usage is more readable than the
> original print()
> * String concatenation with + requires that all arguments are strings,
> which is even less readable
> * With string formatting, you can apply special formatting to your
> inputs (eg. set width, number precision?), which is hard or impossible
> with print()
> * Using print() with commas adds spaces between all entries, which
> might look bad (and it does in this example); the only way to prevent
> that is by setting `sep=`, but then you need to remember about a space
> after "visited" and around the ampersand?
> * Easy to localize (translate into different languages), which is
> generally impossible with any of the other options (some languages
> might rearrange the sentence!)
>
> --
> Chris Warrick <https://chriswarrick.com/>
> PGP: 5EAAEA16
>
>
> ------------------------------
>
> Message: 3
> Date: Sun, 3 Jan 2016 08:07:06 -0500
> From: Francois Dion <francois.dion at gmail.com>
> To: "yehudak ." <katye2007 at gmail.com>
> Cc: "tutor at python.org" <tutor at python.org>
> Subject: Re: [Tutor] To FORMAT or not to
> Message-ID:
>         <CAOLi1KAabyvxJUNgQB1NoOnv__r2Xi_e4Zq=
> z6FOvtRxqdBriw at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> The answer is neither. The second shows the intent in part but doesn't
> quite get it right.
>
> The intent is to have a string template and insert values in that template:
>
> print("You've visited {} & {}.".format(island, new)
>
> This is totally clear what is going to happen. I'm not relying on the
> behaviour of print() to format my string. Format does the formating, print
> the printing :)
>
> This separation of concern is a basic building block of good code and is
> seen at various scale levels. At a higher level, it is seen in concepts
> like MVC.
>
> Another thing you do by having your string separated, is you could have
> them defined elsewhere and have, say, a version in english and a version in
> french.
>
> I;m sure you can see the value.
>
> Francois
>
> On Sun, Jan 3, 2016 at 7:27 AM, yehudak . <katye2007 at gmail.com> wrote:
>
> > Hi there,
> > In a program I wrote the following line (Python 3.5):
> >
> > print("You've visited", island, '&', new + ".")
> >
> > A programmer told me that it's a bad habit, and I should have used
> instead:
> >
> > print("You've visited {0} {1} {2}{3}".format(island, "&", new, "."))
> >
> > May I understand why?
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
> >
>
>
>
> --
> raspberry-python.blogspot.com - www.pyptug.org - www.3DFutureTech.info -
> @f_dion
>
>
> ------------------------------
>
> Message: 4
> Date: Sun, 03 Jan 2016 14:09:24 +0100
> From: Peter Otten <__peter__ at web.de>
> To: tutor at python.org
> Subject: Re: [Tutor] To FORMAT or not to
> Message-ID: <n6b6i7$g37$1 at ger.gmane.org>
> Content-Type: text/plain; charset="ISO-8859-1"
>
> yehudak . wrote:
>
> > Hi there,
> > In a program I wrote the following line (Python 3.5):
> >
> > print("You've visited", island, '&', new + ".")
> >
> > A programmer told me that it's a bad habit, and I should have used
> > instead:
> >
> > print("You've visited {0} {1} {2}{3}".format(island, "&", new, "."))
> >
> > May I understand why?
>
> I don't see the benefits either. If anything I'd move the constants into
> the
> format string:
>
> print("You've visited {island}, & {new}.".format(island=island, new=new))
>
> If you use the same names in your format string and your code it should
> suffice to read the format string to get an idea of what will be printed.
>
> In future versions of Python you can simplify it to
>
> print(f"You've visited {island}, & {new}.")
>
> https://www.python.org/dev/peps/pep-0498/
>
>
>
> ------------------------------
>
> Message: 5
> Date: Sun, 3 Jan 2016 08:10:09 -0500
> From: Francois Dion <francois.dion at gmail.com>
> To: Chris Warrick <kwpolska at gmail.com>
> Cc: "yehudak ." <katye2007 at gmail.com>, "tutor at python.org"
>         <tutor at python.org>
> Subject: Re: [Tutor] To FORMAT or not to
> Message-ID:
>         <
> CAOLi1KB8s53RDKbiSFB-B6_-T7JivZ96DAwFYM+Z+6DDQnYtsQ at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> And as Chris points out, if there is any possibility that the words will be
> in a different order in a different language, use {0}, {1} instead of {}.
>
>
> Francois
>
> On Sun, Jan 3, 2016 at 8:04 AM, Chris Warrick <kwpolska at gmail.com> wrote:
>
> > On 3 January 2016 at 13:27, yehudak . <katye2007 at gmail.com> wrote:
> > > Hi there,
> > > In a program I wrote the following line (Python 3.5):
> > >
> > > print("You've visited", island, '&', new + ".")
> > >
> > > A programmer told me that it's a bad habit, and I should have used
> > instead:
> > >
> > > print("You've visited {0} {1} {2}{3}".format(island, "&", new, "."))
> > >
> > > May I understand why?
> > > _______________________________________________
> > > Tutor maillist  -  Tutor at python.org
> > > To unsubscribe or change subscription options:
> > > https://mail.python.org/mailman/listinfo/tutor
> >
> > The programmer was not very intelligent in his use of str.format in
> > the first place. A more sensible way to write this is:
> >
> > print("You've visited {0} & {1}.".format(island, new))
> >
> > Formatting with constant strings is pointless, just include it in the
> > original input. However, string formatting is not.
> >
> > Here are a couple of reasons:
> > * String formatting works everywhere, but this syntax is specific to
> > print() ? if you use something else, you might end up producing faulty
> > code
> > * The corrected string formatting usage is more readable than the
> > original print()
> > * String concatenation with + requires that all arguments are strings,
> > which is even less readable
> > * With string formatting, you can apply special formatting to your
> > inputs (eg. set width, number precision?), which is hard or impossible
> > with print()
> > * Using print() with commas adds spaces between all entries, which
> > might look bad (and it does in this example); the only way to prevent
> > that is by setting `sep=`, but then you need to remember about a space
> > after "visited" and around the ampersand?
> > * Easy to localize (translate into different languages), which is
> > generally impossible with any of the other options (some languages
> > might rearrange the sentence!)
> >
> > --
> > Chris Warrick <https://chriswarrick.com/>
> > PGP: 5EAAEA16
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
> >
>
>
>
> --
> raspberry-python.blogspot.com - www.pyptug.org - www.3DFutureTech.info -
> @f_dion
>
>
> ------------------------------
>
> Message: 6
> Date: Sun, 3 Jan 2016 13:12:41 +0000
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] To FORMAT or not to
> Message-ID: <n6b6o8$isq$1 at ger.gmane.org>
> Content-Type: text/plain; charset=utf-8
>
> On 03/01/16 12:27, yehudak . wrote:
> > Hi there,
> > In a program I wrote the following line (Python 3.5):
> >
> > print("You've visited", island, '&', new + ".")
> >
> > A programmer told me that it's a bad habit, and I should have used
> instead:
> >
> > print("You've visited {0} {1} {2}{3}".format(island, "&", new, "."))
> >
> > May I understand why?
>
> There are several reasons although your technique is far from
> the worst way of doing things. And the format string here would probably
> be better written as:
>
> print("You've visited {0} & {2}.".format(island, new))
>
> ie only put the variables as placeholders.
>
> Why is it better?
> 1) It is slightly more performant. String addition and
> concatenation are relatively slow processes in Python.
> Formatting will usually be slightly faster. This is not
> a good reason in itself (and in your case with a single
> print it's probably irrelevant) but it's one factor. It
> does matter more if you are printing inside a loop with
> many variables and long strings. (For example assembling
> a web page). So it's a good habit to adopt.
>
> 2) It improves consistency. You can store the format
> string as a variable and then print it out many times
> from different parts of your code and it will always
> be the same. This makes it much easier to maintain
> your program. For example:
>
>     fmtString = "You've visited {0} & {2}."
>     if foo:
>       print(fmtString.format(foo,bar))
>     else:
>       print(fmtString.format(baz,bad))
>
> Now if you want to change the message you only need to
> change the text in one place rather than searching your
> code to do an edit. Also the user sees exactly the same
> formatting, no extra spaces in one message compared
> to another for example.
>
> 3) formatting provides many extra features to control
> justification, leading and spacing. This is especially
> important for numeric output where you can define the
> number of decimal places, whether a leading sign is
> included, padding with zeros etc, etc. You may not
> need that initially but if you do have to add it in,
> it's trivial with a format string but much more work
> if you have lots of hard coded messages all over your
> code.
>
> There may be other reasons too but that should give
> you some ideas.
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> https://mail.python.org/mailman/listinfo/tutor
>
>
> ------------------------------
>
> End of Tutor Digest, Vol 143, Issue 4
> *************************************
>


More information about the Tutor mailing list