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


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

Is there a more elegant way to spell this?

Started byNeal Becker <ndbecker2@gmail.com>
First post2015-01-27 08:15 -0500
Last post2015-01-28 13:16 +1100
Articles 13 — 7 participants

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


Contents

  Is there a more elegant way to spell this? Neal Becker <ndbecker2@gmail.com> - 2015-01-27 08:15 -0500
    Re: Is there a more elegant way to spell this? Rustom Mody <rustompmody@gmail.com> - 2015-01-27 06:08 -0800
    Re: Is there a more elegant way to spell this? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2015-01-27 16:25 +0200
      Re: Is there a more elegant way to spell this? Neal Becker <ndbecker2@gmail.com> - 2015-01-27 09:37 -0500
        Re: Is there a more elegant way to spell this? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2015-01-27 16:47 +0200
      Re: Is there a more elegant way to spell this? Mario Figueiredo <marfig@gmail.com> - 2015-01-27 19:05 +0100
        Re: Is there a more elegant way to spell this? random832@fastmail.us - 2015-01-27 13:13 -0500
      Re: Is there a more elegant way to spell this? Neal Becker <ndbecker2@gmail.com> - 2015-01-27 13:25 -0500
    Re: Is there a more elegant way to spell this? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-28 11:55 +1100
      Re: Is there a more elegant way to spell this? Mario Figueiredo <marfig@gmail.com> - 2015-01-28 02:19 +0100
        Re: Is there a more elegant way to spell this? Ben Finney <ben+python@benfinney.id.au> - 2015-01-28 12:29 +1100
          Re: Is there a more elegant way to spell this? Mario Figueiredo <marfig@gmail.com> - 2015-01-28 09:32 +0100
        Re: Is there a more elegant way to spell this? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-28 13:16 +1100

#84642 — Is there a more elegant way to spell this?

FromNeal Becker <ndbecker2@gmail.com>
Date2015-01-27 08:15 -0500
SubjectIs there a more elegant way to spell this?
Message-ID<mailman.18170.1422364524.18130.python-list@python.org>
Is there a more elegant way to spell this?

for x in [_ for _ in seq if some_predicate]:


-- 
-- Those who don't understand recursion are doomed to repeat it

[toc] | [next] | [standalone]


#84645

FromRustom Mody <rustompmody@gmail.com>
Date2015-01-27 06:08 -0800
Message-ID<93753e86-165b-4e46-a0bd-4e4a419d68d9@googlegroups.com>
In reply to#84642
On Tuesday, January 27, 2015 at 6:45:41 PM UTC+5:30, Neal Becker wrote:
> Is there a more elegant way to spell this?
> 
> for x in [_ for _ in seq if some_predicate]:

Depends on what follows the ':'

In the trivial case all thats outside the comprehension can be dropped:

>>> [x for x in [y for y in range(10) if y % 2 == 0]]
[0, 2, 4, 6, 8]
>>> [y for y in range(10) if y % 2 == 0]
[0, 2, 4, 6, 8]
>>> 

Or

>>> [x*x for x in [y for y in range(10) if y % 2 == 0]]
[0, 4, 16, 36, 64]
>>> [y*y for y in range(10) if y % 2 == 0]
[0, 4, 16, 36, 64]
>>> 
> 
> 
> -- 
> -- Those who don't understand recursion are doomed to repeat it

Ha!

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


#84646

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2015-01-27 16:25 +0200
Message-ID<qot7fw8s3la.fsf@ruuvi.it.helsinki.fi>
In reply to#84642
Neal Becker writes:

> Is there a more elegant way to spell this?
> 
> for x in [_ for _ in seq if some_predicate]:

If you mean some_predicate(_), then possibly this.

for x in filter(some_predicate, seq):
   handle(x)

If you mean literally some_predicate, then perhaps this.

if some_predicate:
   for x in seq:
      handle(x)

Unless you also have in mind an interesting arrangement where
some_predicate might change during the loop, like this.

for x in [_ for _ in seq if some_predicate]:
    ...
    some_predicate = fubar(x)
    ...

Then I have nothing to say.

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


#84647

FromNeal Becker <ndbecker2@gmail.com>
Date2015-01-27 09:37 -0500
Message-ID<mailman.18173.1422369489.18130.python-list@python.org>
In reply to#84646
Jussi Piitulainen wrote:

> Neal Becker writes:
> 
>> Is there a more elegant way to spell this?
>> 
>> for x in [_ for _ in seq if some_predicate]:
> 
> If you mean some_predicate(_), then possibly this.
> 
> for x in filter(some_predicate, seq):
>    handle(x)
> 

I like this best, except probably even better:

for x in ifilter (some_predicate, seq):

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


#84648

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2015-01-27 16:47 +0200
Message-ID<qot386ws2k9.fsf@ruuvi.it.helsinki.fi>
In reply to#84647
Neal Becker writes:
> Jussi Piitulainen wrote:
> > Neal Becker writes:
> > 
> >> Is there a more elegant way to spell this?
> >> 
> >> for x in [_ for _ in seq if some_predicate]:
> > 
> > If you mean some_predicate(_), then possibly this.
> > 
> > for x in filter(some_predicate, seq):
> >    handle(x)
> > 
> 
> I like this best, except probably even better:
> 
> for x in ifilter (some_predicate, seq):

That's in Python 2 and in itertools, I think. In Python 3, filter is a
built-in and returns a filter object.

>>> f = filter(None, map(int, '102030'))
>>> f
<filter object at 0x217d9d0>
>>> next(f)
1
>>> list(f)
[2, 3]
>>> list(f)
[]
>>> 

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


#84659

FromMario Figueiredo <marfig@gmail.com>
Date2015-01-27 19:05 +0100
Message-ID<MPG.2f31f1c4a200b3239896a0@nntp.aioe.org>
In reply to#84646
In article <qot7fw8s3la.fsf@ruuvi.it.helsinki.fi>, 
jpiitula@ling.helsinki.fi says...
> 
> If you mean literally some_predicate, then perhaps this.
> 
> if some_predicate:
>    for x in seq:
>       handle(x)
> 

Careful. See Chris Warrick answer for the correct position of the 'if' 
statement.

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


#84661

Fromrandom832@fastmail.us
Date2015-01-27 13:13 -0500
Message-ID<mailman.18181.1422382422.18130.python-list@python.org>
In reply to#84659
On Tue, Jan 27, 2015, at 13:05, Mario Figueiredo wrote:
> In article <qot7fw8s3la.fsf@ruuvi.it.helsinki.fi>, 
> jpiitula@ling.helsinki.fi says...
> > 
> > If you mean literally some_predicate, then perhaps this.
> > 
> > if some_predicate:
> >    for x in seq:
> >       handle(x)
> > 
> 
> Careful. See Chris Warrick answer for the correct position of the 'if' 
> statement.

I think by "if you mean literally some_predicate" he was taking
some_predicate as a variable [rather than an expression] that does not
change during the loop. That'd be a silly thing to do in the originally
posted code, though, since it'd be easier to do "[] if not
some_predicate else [...]"

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


#84662

FromNeal Becker <ndbecker2@gmail.com>
Date2015-01-27 13:25 -0500
Message-ID<mailman.18182.1422383167.18130.python-list@python.org>
In reply to#84646
Jussi Piitulainen wrote:

> Neal Becker writes:
> 
>> Is there a more elegant way to spell this?
>> 
>> for x in [_ for _ in seq if some_predicate]:
> 
> If you mean some_predicate(_), then possibly this.
> 
> for x in filter(some_predicate, seq):
>    handle(x)
> 
> If you mean literally some_predicate, then perhaps this.
> 
> if some_predicate:
>    for x in seq:
>       handle(x)
> 
> Unless you also have in mind an interesting arrangement where
> some_predicate might change during the loop, like this.
> 
> for x in [_ for _ in seq if some_predicate]:
>     ...
>     some_predicate = fubar(x)
>     ...
> 
> Then I have nothing to say.


To clarify, I meant some_predicate(_), and then ifilter looks like a nice 
solution.

-- 
-- Those who don't understand recursion are doomed to repeat it

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


#84695

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2015-01-28 11:55 +1100
Message-ID<54c8339f$0$13008$c3e8da3$5496439d@news.astraweb.com>
In reply to#84642
Neal Becker wrote:

> Is there a more elegant way to spell this?
> 
> for x in [_ for _ in seq if some_predicate]:

Don't use _ as the loop variable here.

There are three common conventions for _ and this is none of them:


(1) n the interactive interpreter _ is used for the result of the last
expression:

py> 1+2
3
py> _ * 2
6

(2) In locale-aware applications, _ is apparently used as a function for
localising text into the user's native language.

(3) _ is also commonly used as a "don't care" variable name:

a, _, b, _ = get_four_items()  # but I only care about two of them


So in a loop, you would only use _ as the loop variable when you don't use
the loop variable, e.g.:

[random.random() for _ in range(10)]


Of course, a convention is just a convention, not a law, you can ignore such
conventions if you insist. But conventions make it easier and quicker to
understand code, not just for others, but for yourself as well.


-- 
Steven

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


#84699

FromMario Figueiredo <marfig@gmail.com>
Date2015-01-28 02:19 +0100
Message-ID<MPG.2f3257584e3201d69896af@nntp.aioe.org>
In reply to#84695
In article <54c8339f$0$13008$c3e8da3$5496439d@news.astraweb.com>, 
steve+comp.lang.python@pearwood.info says...
> (3) _ is also commonly used as a "don't care" variable name:
> 
> a, _, b, _ = get_four_items()  # but I only care about two of them
> 

According to the following link, it is actually a double underscore:
http://docs.python-guide.org/en/latest/writing/style/#idioms

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


#84701

FromBen Finney <ben+python@benfinney.id.au>
Date2015-01-28 12:29 +1100
Message-ID<mailman.18197.1422408555.18130.python-list@python.org>
In reply to#84699
Mario Figueiredo <marfig@gmail.com> writes:

> In article <54c8339f$0$13008$c3e8da3$5496439d@news.astraweb.com>, 
> steve+comp.lang.python@pearwood.info says...
> > (3) _ is also commonly used as a "don't care" variable name:
> > 
> > a, _, b, _ = get_four_items()  # but I only care about two of them
> > 
>
> According to the following link, it is actually a double underscore:
> http://docs.python-guide.org/en/latest/writing/style/#idioms

More accurately (and as acknowledged in that guide), a single underscore
*is* a common name for a “don't care” name, but is better avoided for
that purpose because it's also commonly used for other purposes.

In other words: That guide is correct in its admonition, but that
doesn't challenge what Steven said about common usage.

-- 
 \       “My business is to teach my aspirations to conform themselves |
  `\              to fact, not to try and make facts harmonise with my |
_o__)                   aspirations.“ —Thomas Henry Huxley, 1860-09-23 |
Ben Finney

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


#84728

FromMario Figueiredo <marfig@gmail.com>
Date2015-01-28 09:32 +0100
Message-ID<MPG.2f32bd1e986d67d99896b0@nntp.aioe.org>
In reply to#84701
In article <mailman.18197.1422408555.18130.python-list@python.org>, 
ben+python@benfinney.id.au says...
> 
> More accurately (and as acknowledged in that guide), a single underscore
> *is* a common name for a ?don't care? name, but is better avoided for
> that purpose because it's also commonly used for other purposes.
> 
> In other words: That guide is correct in its admonition, but that
> doesn't challenge what Steven said about common usage.

I was not trying to challenge his assertion. Only adding more 
information to it.

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


#84705

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2015-01-28 13:16 +1100
Message-ID<54c8467e$0$13013$c3e8da3$5496439d@news.astraweb.com>
In reply to#84699
Mario Figueiredo wrote:

> In article <54c8339f$0$13008$c3e8da3$5496439d@news.astraweb.com>,
> steve+comp.lang.python@pearwood.info says...
>> (3) _ is also commonly used as a "don't care" variable name:
>> 
>> a, _, b, _ = get_four_items()  # but I only care about two of them
>> 
> 
> According to the following link, it is actually a double underscore:
> http://docs.python-guide.org/en/latest/writing/style/#idioms

That's third-party documentation, not official, and I strongly disagree with
a couple of those recommendations. But this specific one seems reasonable
enough.


-- 
Steven

[toc] | [prev] | [standalone]


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


csiph-web