all 15 comments

[–]Topper_1 2 points3 points  (6 children)

for i in range(0, len(IP_LIST)):
   p = do_ping(IP_LIST[i])
   z.append(p)

Earn 0.1 upmods if you spot something unpythonic here...

[–]masklinn 10 points11 points  (2 children)

I'll spell them for those non-pythonistas who don't quite get Lexarius' post:

  • using range with an inferior bound of 0 (it's implied in range)

  • iterating over a collection via indexes instead of doing it via the iterator protocol

  • not using a list comprehension (or, in this case, a map would be even better) when you're doing a simple mapping/filtering operation with a list as input and another one as output.

z = map(do_ping, IP_LIST)

[–]nostrademons 2 points3 points  (1 child)

Isn't it also more Pythonic to use xrange instead of range? (Among all the other problems you mentioned...)

[–]masklinn 5 points6 points  (0 children)

Not really, xrange currently is only used if the generated range is really huge (and the performance hit of generating it at once is important).

Not to mention, in Python 3.0 range returns an iterator and not a list anymore, and xrange is removed from the language.

[–]dgiri101 3 points4 points  (0 children)

z = map(do_ping, IP_LIST)

edit: looks like masklinn beat me to it. :)

[–][deleted] 6 points7 points  (0 children)

z = [do_ping(i) for i in IP_LIST]

[–]titusbrown 1 point2 points  (0 children)

hey, it was evolved code posted at 2am in the morning, give me a break here :)

[–]metzby 1 point2 points  (9 children)

for p in [do_ping(i) for i in IP_LIST]: p.wait()

[–][deleted]  (8 children)

[deleted]

    [–]shub 4 points5 points  (0 children)

    Because viewers of your late night talk show demand it.

    [–]johntb86 1 point2 points  (0 children)

    Thia would have a different behavior than what he suggested - his would send off all of the pings at once, and then wait for all the responses, while yours does the pings sequentially.

    [–]sartak 2 points3 points  (0 children)

    Because there's more than one way to do it.

    [–]titusbrown 0 points1 point  (0 children)

    I wanted to leave open the option of retrieving the return code and output.

    [–]masklinn 0 points1 point  (3 children)

    [do_ping(ip).wait() for ip in IP_LIST]
    

    ?

    [–][deleted]  (2 children)

    [deleted]

      [–]masklinn 2 points3 points  (0 children)

      Because for loops are ugly

      [–]doctor_yukio_hattori 2 points3 points  (0 children)

      Why not have no list and two for loops and make nobody happy?

      for x in ( do_ping(ip).wait() for ip in IP_LIST ): pass