Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why do this?

Reply
Thread Tools

Why do this?

 
 
Matthew Warren
Guest
Posts: n/a
 
      10-05-2006
Ok, not really python focused, but it feels like the people here could
explain it for me

Now, I started programming when I was 8 with BBC Basic.

I never took any formal classes however, and I have never become an
expert programmer. I'm an average/hobbyist programmer with quite a few
languages under my belt but I cant do any really fancy tricks with any
of them. (although Python might be nudging me into more advanced things,
now I'm starting to get what all the __method__ thingies and operators
are all about)

I learned over the years to do things like the following, and I like
doing it like this because of readability, something Python seems to
focus on :-

Print "There are "+number+" ways to skin a "+furryanimal

But nowadays, I see things like this all over the place;

print("There are %s ways to skin a %s" % (number, furryanimal))

Now I understand there can be additional formatting benefits when
dealing with numbers, decimal places etc.. But to me, for strings, the
second case is much harder to read than the first.

I hope I'm not being dense.

The result is that I have pathalogically avoided print "%s" % (thing)
because it seems to just over complicate things.


Ta,

Matt.





This email is confidential and may be privileged. If you are not the intended recipient please notify the sender immediately and delete the email from your computer.

You should not copy the email, use it for any purpose or disclose its contents to any other person.
Please note that any views or opinions presented in this email may be personal to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence of viruses. Digica accepts no liability for any damage caused by any virus transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      10-05-2006
"Matthew Warren" <(E-Mail Removed)> wrote:

> Print "There are "+number+" ways to skin a "+furryanimal
>
> But nowadays, I see things like this all over the place;
>
> print("There are %s ways to skin a %s" % (number, furryanimal))
>
> Now I understand there can be additional formatting benefits when
> dealing with numbers, decimal places etc.. But to me, for strings, the
> second case is much harder to read than the first.
>


apart from the spurious parentheses you added in the second one, you also
missed out this variant:

print "There are", number, "ways to skin a", furryanimal

That only works for print though, not for other uses of strings, but it is
related to the main reason I use format strings instead of concatenation.

The problem I have with your first option is the large number of times I've
written:

print "There are"+number+"ways to skin a"+furryanimal

or at least something equivalent to it. If I try to make the same mistake
with a format string it jumps out to me as wrong:

"There are%sways to skin a%s" % (number, furryanimal)

Also, having a variable of type str called 'number' seems perverse (and
probably error prone), so I suspect I might need something like:

print "There are "+str(number)+" ways to skin a "+furryanimal

but the format string does the conversion for free.

The other main reason for preferring format strings is that they make it
easier to refactor the code. If you ever want to move the message away from
where the formatting is done then it's a lot easier to extract a single
string than it is to clean up the concatenation.
 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      10-05-2006
In <(E-Mail Removed)>, Matthew Warren
wrote:

> I learned over the years to do things like the following, and I like
> doing it like this because of readability, something Python seems to
> focus on :-
>
> Print "There are "+number+" ways to skin a "+furryanimal
>
> But nowadays, I see things like this all over the place;
>
> print("There are %s ways to skin a %s" % (number, furryanimal))
>
> Now I understand there can be additional formatting benefits when
> dealing with numbers, decimal places etc.. But to me, for strings, the
> second case is much harder to read than the first.


For me it's the other way around -- I find the second one more readable
especially without syntax highlighting to see which characters are inside
and which are outside the quotes.

With the second one there's a clear visual separation of the string
literal and the variables, even without colors.

Another advantage of the second way is much easier localization and other
tasks where the sentence itself is not hardcoded but read from files,
databases, etc.

And the '%s' automatically converts the objects to strings. The
equivalent to the second example without string formatting would be::

print 'There are' + str(number) + ' ways to skin a ' + str(furryanimal)

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
Ivan Voras
Guest
Posts: n/a
 
      10-05-2006
Duncan Booth wrote:

> print "There are"+number+"ways to skin a"+furryanimal
>
> or at least something equivalent to it. If I try to make the same mistake
> with a format string it jumps out to me as wrong:
>
> "There are%sways to skin a%s" % (number, furryanimal)


Related to this, formatting with sequences is also much more readable
when there are complex interpunction and quoting characters present,
like this:

print "'"+var1+"','"+var2'"+","+var3

the above is much more readable as

print "'%s', '%s', %s" % (var1, var2, var3)
 
Reply With Quote
 
Maric Michaud
Guest
Posts: n/a
 
      10-05-2006
Le jeudi 05 octobre 2006 13:16, Ivan Voras a écrit*:
> print "'"+var1+"','"+var2'"+","+var3
>
> the above is much more readable as
>
> print "'%s', '%s', %s" % (var1, var2, var3)


It feels not IMO, one proof I see is that you forgot the spaces after periods
in your first example, and it's even not easy to figure it out...

In fact this is a typical case I replace the first by the second, when the
string comes a little complex.

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
 
Reply With Quote
 
Corrado Gioannini
Guest
Posts: n/a
 
      10-05-2006
On Thu, Oct 05, 2006 at 10:48:36AM +0000, Duncan Booth wrote:
> The other main reason for preferring format strings is that they make it
> easier to refactor the code. If you ever want to move the message away from
> where the formatting is done then it's a lot easier to extract a single
> string than it is to clean up the concatenation.


This is a good point imho.
I often do things like this:

sql = a_complex_select_sql % (id_foo, value_bar, ...)
cursor.execute(sql)

inside the body of a function (or a class method), where
a_complex_select_sql is a string, containing several %s, %d ecc.,
that is defined globally (or somewhere else in the class).

c.
--
"Thought is only a flash between two long nights,
but this flash is everything."
(H. Poincaré)
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      10-06-2006
On Thu, 5 Oct 2006 11:28:08 +0100, "Matthew Warren"
<(E-Mail Removed)> declaimed the following in comp.lang.python:

>
> Now, I started programming when I was 8 with BBC Basic.
>

Remember what the acronym BASIC stands for?

>
> I learned over the years to do things like the following, and I like
> doing it like this because of readability, something Python seems to
> focus on :-
>
> Print "There are "+number+" ways to skin a "+furryanimal
>

I'd consider that a convenience method (heck, "print" [Python is
case sensitive] is a convenience statement -- the formal statement would
be sys.stdout.write(....)).

Many major languages (going back to FORTRAN, COBOL, C, Ada) require
explicit formatting control. In Ada the above would probably be
something like:

put('There are '); -- I forget if Ada uses " or ' for strings
put(number);
put(' ways to skin a ');
put_line(furryanimal);
{granted, if it is known that all items are strings, then:
put_line('There are ' &
number &
' ways to skin a ' &
furryanimal);
}

> But nowadays, I see things like this all over the place;
>
> print("There are %s ways to skin a %s" % (number, furryanimal))
>


At present, the outer ( ) are superfluous. The main features are
that one can do more than %s... %20s makes a field 20 characters wide...
%4.4x makes a zero-filled hexadecimal value... And one can easily
"internationalize" output by reading formats from a data file...

mess_skinning = ifile.readline() #where ifile is opened to the
#needed language


print mess_skinning % (number, furryanimal)
--
Wulfraed Dennis Lee Bieber KD6MOG
http://www.velocityreviews.com/forums/(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/
 
Reply With Quote
 
hanumizzle
Guest
Posts: n/a
 
      10-06-2006
On 10/6/06, Dennis Lee Bieber <(E-Mail Removed)> wrote:
> On Thu, 5 Oct 2006 11:28:08 +0100, "Matthew Warren"
> <(E-Mail Removed)> declaimed the following in comp.lang.python:
>
> >
> > Now, I started programming when I was 8 with BBC Basic.
> >

> Remember what the acronym BASIC stands for?


Boobie-drawing Adolescent Symbolic Instruction Code.

-- Theerasak
 
Reply With Quote
 
Hendrik van Rooyen
Guest
Posts: n/a
 
      10-06-2006
"Dennis Lee Bieber" <(E-Mail Removed)> wrote:

> On Thu, 5 Oct 2006 11:28:08 +0100, "Matthew Warren"
> <(E-Mail Removed)> declaimed the following in comp.lang.python:
>
> >
> > Now, I started programming when I was 8 with BBC Basic.
> >

> Remember what the acronym BASIC stands for?


8<-----------------------------------------------

yes

- Hendrik


 
Reply With Quote
 
half.italian@gmail.com
Guest
Posts: n/a
 
      10-06-2006
Nobody's mentioned the ability to save a formatted string and then
substitute the variables later...

string = "There are %s ways to skin a %s"

print string % (3, "furry animal")
print string % (166, "beast")

~half.italian

Matthew Warren wrote:
> Ok, not really python focused, but it feels like the people here could
> explain it for me
>
> Now, I started programming when I was 8 with BBC Basic.
>
> I never took any formal classes however, and I have never become an
> expert programmer. I'm an average/hobbyist programmer with quite a few
> languages under my belt but I cant do any really fancy tricks with any
> of them. (although Python might be nudging me into more advanced things,
> now I'm starting to get what all the __method__ thingies and operators
> are all about)
>
> I learned over the years to do things like the following, and I like
> doing it like this because of readability, something Python seems to
> focus on :-
>
> Print "There are "+number+" ways to skin a "+furryanimal
>
> But nowadays, I see things like this all over the place;
>
> print("There are %s ways to skin a %s" % (number, furryanimal))
>
> Now I understand there can be additional formatting benefits when
> dealing with numbers, decimal places etc.. But to me, for strings, the
> second case is much harder to read than the first.
>
> I hope I'm not being dense.
>
> The result is that I have pathalogically avoided print "%s" % (thing)
> because it seems to just over complicate things.
>
>
> Ta,
>
> Matt.
>
>
>
>
>
> This email is confidential and may be privileged. If you are not the intended recipient please notify the sender immediately and delete the email from your computer.
>
> You should not copy the email, use it for any purpose or disclose its contents to any other person.
> Please note that any views or opinions presented in this email may be personal to the author and do not necessarily represent the views or opinions of Digica.
> It is the responsibility of the recipient to check this email for the presence of viruses. Digica accepts no liability for any damage caused by any virus transmitted by this email.
>
> UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
> Reception Tel: + 44 (0) 115 977 1177
> Support Centre: 0845 607 7070
> Fax: + 44 (0) 115 977 7000
> http://www.digica.com
>
> SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South Africa
> Tel: + 27 (0) 21 957 4900
> Fax: + 27 (0) 21 948 3135
> http://www.digica.com


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments