[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Making string-formatting smarter by handling generators?

D'Arcy J.M. Cain

2/27/2008 5:26:00 PM

On Wed, 27 Feb 2008 10:23:49 -0600
Tim Chase <python.list@tim.thechases.com> wrote:
> I can force it by wrapping the results of my generator in a call
> to tuple() or list()

I think you are wrong about list(). Since map() returns a list already
it doesn't change anything.

> print "%s, %s" % tuple(map(transform, pair))

Yes, it works.

> but it feels a bit hackish to me.

I can't imagine what else you could do to turn a list into a tuple that
would be less hackish than simply making it a tuple.

> I find I hit it mostly with calls to map() where I want to apply
> some transform (as above) to all the items in a list of
> parameters such as
>
> "%s=%s&%s=%s" % map(urllib.quote, params)

Isn't map() deprecated? The above can be done with;

"%s=%s&%s=%s" % tuple([urllib.quote(x) for x in params])

> Any suggestions? (even if it's just "get over your hangup with
> wrapping the results in list()/tuple()" :)

Pretty much. :-)

--
D'Arcy J.M. Cain <darcy@druid.net> | Democracy is three wolves
http://www.druid.... | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
3 Answers

Arnaud Delobelle

2/27/2008 9:06:00 PM

0

On Feb 27, 5:25 pm, "D'Arcy J.M. Cain" <da...@druid.net> wrote:
> On Wed, 27 Feb 2008 10:23:49 -0600
>
> Tim Chase <python.l...@tim.thechases.com> wrote:
>
> >    "%s=%s&%s=%s" % map(urllib.quote, params)
>
> Isn't map() deprecated?  The above can be done with;
>
>     "%s=%s&%s=%s" % tuple([urllib.quote(x) for x in params])

I don't think that map() is deprecated. In python 3.0 it is still
present, only it returns an iterator instead of a list.

In this case

map(urllib.quote, params)

looks for urlib.quote exactly once altogether. Whereas

[urllib.quote(x) for x in params]

looks for urlib.quote once for every element in params. (Of course in
the example given params only has 4 elements so it doesn't matter).

--
Arnaud

Ben Finney

2/27/2008 9:52:00 PM

0

"D'Arcy J.M. Cain" <darcy@druid.net> writes:

> The above can be done with;
>
> "%s=%s&%s=%s" % tuple([urllib.quote(x) for x in params])

Or, better::

"%s=%s&%s=%s" % tuple(urllib.quote(x) for x in params)

passing a generator, instead of a list created from a generator, to
the tuple constructor.

--
\ "I put instant coffee in a microwave oven and almost went back |
`\ in time." -- Steven Wright |
_o__) |
Ben Finney

D'Arcy J.M. Cain

2/27/2008 10:49:00 PM

0

On Wed, 27 Feb 2008 13:06:27 -0800 (PST)
Arnaud Delobelle <arnodel@googlemail.com> wrote:
> On Feb 27, 5:25 pm, "D'Arcy J.M. Cain" <da...@druid.net> wrote:
> > Isn't map() deprecated?  The above can be done with;
> I don't think that map() is deprecated. In python 3.0 it is still
> present, only it returns an iterator instead of a list.

Discouraged then? Our benign dictator has said that filter and map
must die. Others suggest that map is not as efficient.

--
D'Arcy J.M. Cain <darcy@druid.net> | Democracy is three wolves
http://www.druid.... | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.