Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75285 > unrolled thread

What meaning of this ""hello %s you are %s years old" % x"

Started byfl <rxjwg98@gmail.com>
First post2014-07-27 11:49 -0700
Last post2014-07-28 17:58 +1000
Articles 5 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  What meaning of this ""hello %s you are %s years old" % x" fl <rxjwg98@gmail.com> - 2014-07-27 11:49 -0700
    Re: What meaning of this ""hello %s you are %s years old" % x" Gary Herron <gary.herron@islandtraining.com> - 2014-07-27 13:02 -0700
    Re: What meaning of this ""hello %s you are %s years old" % x" Dan Stromberg <drsalists@gmail.com> - 2014-07-27 14:37 -0700
    Re: What meaning of this ""hello %s you are %s years old" % x" Albert-Jan Roskam <fomcl@yahoo.com> - 2014-07-28 00:41 -0700
    Re: What meaning of this ""hello %s you are %s years old" % x" Chris Angelico <rosuav@gmail.com> - 2014-07-28 17:58 +1000

#75285 — What meaning of this ""hello %s you are %s years old" % x"

Fromfl <rxjwg98@gmail.com>
Date2014-07-27 11:49 -0700
SubjectWhat meaning of this ""hello %s you are %s years old" % x"
Message-ID<cefbd1ff-b65f-43ec-947e-2324085a6486@googlegroups.com>
Hi,

I get a post on tuple, see below please, on line. It seems that something is 
missing. I want to learn tuple from this example as well.

Could you explain it to me (a tuple % another tuple)?


Thanks,






http://stackoverflow.com/questions/1708510/python-list-vs-tuple-when-to-use-each

In Python, when should you use lists and when tuples?

Sometimes you don't have a choice, for example if you have

"hello %s you are %s years old" % x
then x must be a tuple.

But if I am the one who designs the API and gets to choose the data types, then 
what are the guidelines?

[toc] | [next] | [standalone]


#75289

FromGary Herron <gary.herron@islandtraining.com>
Date2014-07-27 13:02 -0700
Message-ID<mailman.12371.1406491903.18130.python-list@python.org>
In reply to#75285
On 07/27/2014 11:49 AM, fl wrote:
> Hi,
>
> I get a post on tuple, see below please, on line. It seems that something is
> missing. I want to learn tuple from this example as well.
>
> Could you explain it to me (a tuple % another tuple)?
>
>
> Thanks,
>
>
>
>
>
>
> http://stackoverflow.com/questions/1708510/python-list-vs-tuple-when-to-use-each
>
> In Python, when should you use lists and when tuples?
>
> Sometimes you don't have a choice, for example if you have
>
> "hello %s you are %s years old" % x
> then x must be a tuple.
>
> But if I am the one who designs the API and gets to choose the data types, then
> what are the guidelines?

That's not tuple%tuple, but rather string%tuple.  And string%tuple is 
the older method of formatting an output string from a template and a 
tuple of values.  See 
https://docs.python.org/2/library/stdtypes.html#string-formatting for 
details.

However, if you are just learning Python, you should probably use the 
*newer* formatting operations.  See 
https://docs.python.org/3.3/library/string.html#formatspec for details 
of that.

Gary Herron

[toc] | [prev] | [next] | [standalone]


#75291

FromDan Stromberg <drsalists@gmail.com>
Date2014-07-27 14:37 -0700
Message-ID<mailman.12372.1406497047.18130.python-list@python.org>
In reply to#75285
On Sun, Jul 27, 2014 at 11:49 AM, fl <rxjwg98@gmail.com> wrote:
> In Python, when should you use lists and when tuples?
>
> Sometimes you don't have a choice, for example if you have
>
> "hello %s you are %s years old" % x
> then x must be a tuple.
>
> But if I am the one who designs the API and gets to choose the data types, then
> what are the guidelines?

You should use a tuple when you need something immutable (readonly),
like a dictionary key.  Immutable objects are good for hashing.

You should use a list when you need something mutable (read/write),
like appending over and over.  Mutable objects are not good for
hashing, because their hash value could change.

[toc] | [prev] | [next] | [standalone]


#75304

FromAlbert-Jan Roskam <fomcl@yahoo.com>
Date2014-07-28 00:41 -0700
Message-ID<mailman.12382.1406533649.18130.python-list@python.org>
In reply to#75285

<snip>
 
> That's not tuple%tuple, but rather string%tuple.  And string%tuple is 
> the older method of formatting an output string from a template and a 
> tuple of values.  See 
> https://docs.python.org/2/library/stdtypes.html#string-formatting for 
> details.
> 
> However, if you are just learning Python, you should probably use the 
> *newer* formatting operations.  See 
> https://docs.python.org/3.3/library/string.html#formatspec for details 
> of that.

Do you know what was the reason/consideration to switch to a new formatting operation? Ability to have custom formatters with an own __format__ method?

Regards,
Albert-Jan

[toc] | [prev] | [next] | [standalone]


#75306

FromChris Angelico <rosuav@gmail.com>
Date2014-07-28 17:58 +1000
Message-ID<mailman.12384.1406534294.18130.python-list@python.org>
In reply to#75285
On Mon, Jul 28, 2014 at 5:41 PM, Albert-Jan Roskam
<fomcl@yahoo.com.dmarc.invalid> wrote:
>> That's not tuple%tuple, but rather string%tuple.  And string%tuple is
>> the older method of formatting an output string from a template and a
>> tuple of values.  See
>> https://docs.python.org/2/library/stdtypes.html#string-formatting for
>> details.
>>
>> However, if you are just learning Python, you should probably use the
>> *newer* formatting operations.  See
>> https://docs.python.org/3.3/library/string.html#formatspec for details
>> of that.
>
> Do you know what was the reason/consideration to switch to a new formatting operation? Ability to have custom formatters with an own __format__ method?

Flexibility. You can do a few things with the other formatting style
that you can't do with percent-formatting. However, percent formatting
isn't going anywhere, and there's no particular reason to avoid it,
even in brand new code. It's more portable across languages (heaps of
C-inspired languages have a printf-style function that responds to the
same notations), more compact, and ample to a lot of situations, so
there's no need to go for the other style.

The only real downside of percent formatting is that, since it's an
operator rather than a function call, it can take only one argument -
so there's some magic with tuples, and a few extremely obscure corner
cases as a result. Not a reason to avoid it.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web