Re: is this pythonic?

Discussion in 'Python' started by Bill Mill, Jul 20, 2005.

  1. Bill Mill

    Bill Mill Guest

    On 7/20/05, Simon Brunning <> wrote:
    > On 7/20/05, Mage <> wrote:
    > > Or is there better way?
    > >
    > > for (i, url) in [(i,links) for i in range(len(links))]:

    >
    > for i, url in enumerate(links):
    >


    +2 for creating seeing a need and crafting a reasonable solution, but
    -1 for not reading the section on builtins to see if it existed
    already.

    (As for its pythonicity, I would have recommended isolating it into a
    function and making it a generator:

    def my_enumerate(enumerable):
    i = 0
    for elt in enumerable:
    yield (i, elt)
    i += 1

    for i, url in my_enumerate(links):

    but it's not too bad as it is. Also, my function is completely
    untested - it's close to right though.)

    Peace
    Bill Mill
     
    Bill Mill, Jul 20, 2005
    #1
    1. Advertising

  2. Bill Mill

    Bill Mill Guest

    On 7/21/05, Steven D'Aprano <> wrote:
    > On Wed, 20 Jul 2005 16:30:10 -0400, Bill Mill wrote:
    >
    > > On 7/20/05, Simon Brunning <> wrote:
    > >> On 7/20/05, Mage <> wrote:
    > >> > Or is there better way?
    > >> >
    > >> > for (i, url) in [(i,links) for i in range(len(links))]:
    > >>
    > >> for i, url in enumerate(links):
    > >>

    > >
    > > +2 for creating seeing a need and crafting a reasonable solution, but
    > > -1 for not reading the section on builtins to see if it existed
    > > already.

    >
    > To see if *what* existed already?
    >
    > It is well and good to say RTFM, but there are 697 subsections to the
    > Python Library reference, and if you don't know what you are looking for,
    > and beginners rarely are, it isn't obvious which is the right section to
    > read. And the Library Reference isn't even "the" manual: there is also the
    > global module reference and language reference.
    >
    > If you already know what you are looking for, reading the manual is great
    > advice. Browsing the manual looking for interesting tidbits can even be
    > fun for a certain mindset. But if you don't know enough to know what to
    > look for, where in the 2000-odd sections of the Python references will
    > you find it?
    >


    I said the *builtins* section. I think you learn pretty quick that
    figuring out what functions are builtins is pretty important in every
    language. There's a fair number of people out there giving the advice
    to read chapter 2 of the library reference cover-to-cover for a good
    starter on python.

    Furthermore, I wasn't being hard on the guy, he still added up to +1.
    Lighten up, I was joking.

    >
    >
    > > (As for its pythonicity, I would have recommended isolating it into a
    > > function and making it a generator:

    >
    > It is easy to take this to extremes. It isn't necessary to isolate
    > everything into its own object, or class, or module. Too much
    > encapsulation is just as bad as too little.
    >


    agreed; his listcomp just looks awkward inside the for loop statement;
    if it were my code, I would put it into a function. He asked if his
    code was pythonic, and I think the (non-extreme) pythonic thing to do
    would be to put his listcomp into a function.

    >
    > > def my_enumerate(enumerable):
    > > i = 0
    > > for elt in enumerable:
    > > yield (i, elt)
    > > i += 1
    > >
    > > for i, url in my_enumerate(links):
    > >
    > > but it's not too bad as it is. Also, my function is completely
    > > untested - it's close to right though.)

    >
    > What is the advantage of your function my_enumerate over the Python
    > built-in enumerate?
    >
    >


    absolutely none; I just was saying how I would encapsulate it into a function.

    Peace
    Bill Mill
    bill.mill at gmail.com
     
    Bill Mill, Jul 21, 2005
    #2
    1. Advertising

  3. On Wed, 20 Jul 2005 16:30:10 -0400, Bill Mill wrote:

    > On 7/20/05, Simon Brunning <> wrote:
    >> On 7/20/05, Mage <> wrote:
    >> > Or is there better way?
    >> >
    >> > for (i, url) in [(i,links) for i in range(len(links))]:

    >>
    >> for i, url in enumerate(links):
    >>

    >
    > +2 for creating seeing a need and crafting a reasonable solution, but
    > -1 for not reading the section on builtins to see if it existed
    > already.


    To see if *what* existed already?

    It is well and good to say RTFM, but there are 697 subsections to the
    Python Library reference, and if you don't know what you are looking for,
    and beginners rarely are, it isn't obvious which is the right section to
    read. And the Library Reference isn't even "the" manual: there is also the
    global module reference and language reference.

    If you already know what you are looking for, reading the manual is great
    advice. Browsing the manual looking for interesting tidbits can even be
    fun for a certain mindset. But if you don't know enough to know what to
    look for, where in the 2000-odd sections of the Python references will
    you find it?



    > (As for its pythonicity, I would have recommended isolating it into a
    > function and making it a generator:


    It is easy to take this to extremes. It isn't necessary to isolate
    everything into its own object, or class, or module. Too much
    encapsulation is just as bad as too little.


    > def my_enumerate(enumerable):
    > i = 0
    > for elt in enumerable:
    > yield (i, elt)
    > i += 1
    >
    > for i, url in my_enumerate(links):
    >
    > but it's not too bad as it is. Also, my function is completely
    > untested - it's close to right though.)


    What is the advantage of your function my_enumerate over the Python
    built-in enumerate?


    --
    Steven.
     
    Steven D'Aprano, Jul 21, 2005
    #3
  4. Steven D'Aprano wrote:

    > The great thing about Usenet and the Internet is that we can pick each
    > other's brains for answers, instead of flailing around blindly in manuals
    > that don't understand the simplest natural language query. And isn't that
    > why we're here?


    Personally, I feel my time is better served by answering questions that
    would not be easy to find without assistance. I can't expect everyone to
    know about or expect enumerate() from the beginning, so I don't have any
    objections to it being asked here.

    If people were to ask what the function signature for enumerate() was
    when that is easy to Google, then I would think they were wasting
    everyone's time.
    --
    Michael Hoffman
     
    Michael Hoffman, Jul 21, 2005
    #4
  5. On Thu, 21 Jul 2005 10:27:24 -0400, Bill Mill wrote:

    [snip]

    > I said the *builtins* section. I think you learn pretty quick that
    > figuring out what functions are builtins is pretty important in every
    > language. There's a fair number of people out there giving the advice
    > to read chapter 2 of the library reference cover-to-cover for a good
    > starter on python.


    Sure. But for a beginner to start learning the language by reading the
    language manual is a bit much to ask. Some people can do it, but most
    learn best by doing, not by reading dry, abstract descriptions of what
    various functions do. In my experience, iterators and generators don't
    even make sense until you've spent some time playing with them.

    > Furthermore, I wasn't being hard on the guy, he still added up to +1.
    > Lighten up, I was joking.


    There is no need to get defensive, I was merely commenting on the need to
    understand that inexperienced programmers often don't know enough about
    the language to know where to start looking for the answer.

    In fact, it isn't just inexperienced programmers, but experienced
    programmers too. I'm sure Guido doesn't need to look up enumerate in the
    reference manual; but if he wanted to write a program to calculate the
    positions of the anti-nodes of vibratory modes of the bound-state of a
    muon and a proton/neutron pair, the odds are pretty good he wouldn't even
    know where to start looking either :)

    The great thing about Usenet and the Internet is that we can pick each
    other's brains for answers, instead of flailing around blindly in manuals
    that don't understand the simplest natural language query. And isn't that
    why we're here?



    Regards,



    --
    Steven.
     
    Steven D'Aprano, Jul 21, 2005
    #5
  6. On Thu, 21 Jul 2005 16:43:00 +0100, Michael Hoffman wrote:

    > Personally, I feel my time is better served by answering questions that
    > would not be easy to find without assistance. I can't expect everyone to
    > know about or expect enumerate() from the beginning, so I don't have any
    > objections to it being asked here.
    >
    > If people were to ask what the function signature for enumerate() was
    > when that is easy to Google, then I would think they were wasting
    > everyone's time.


    And on that, I think we can agree!


    --
    Steven.
     
    Steven D'Aprano, Jul 21, 2005
    #6
  7. Bill Mill

    Terry Reedy Guest

    "Steven D'Aprano" <> wrote in message
    news:p...
    > On Thu, 21 Jul 2005 10:27:24 -0400, Bill Mill wrote:
    >
    > [snip]
    >
    >> I said the *builtins* section. I think you learn pretty quick that
    >> figuring out what functions are builtins is pretty important in every
    >> language. There's a fair number of people out there giving the advice
    >> to read chapter 2 of the library reference cover-to-cover for a good
    >> starter on python.


    Including me.
    >
    > Sure. But for a beginner to start learning the language by reading the
    > language manual is a bit much to ask.


    Sure, but who is suggesting that. Suggesting that people specifically read
    chapter 2 of the Library manual (but only that to start -- I probably have
    still not read every chapter yet) is quite different.

    > There is no need to get defensive, I was merely commenting on the need to
    > understand that inexperienced programmers often don't know enough about
    > the language to know where to start looking for the answer.


    Which is precise why I try to help by telling them where to start. A large
    number of beginner queries posted here are answered in the builtins
    chapter.

    Terry J. Reedy
     
    Terry Reedy, Jul 21, 2005
    #7
  8. On Thu, Jul 21, 2005 at 10:27:24AM -0400, Bill Mill wrote:
    > On 7/21/05, Steven D'Aprano <> wrote:
    > > On Wed, 20 Jul 2005 16:30:10 -0400, Bill Mill wrote:
    > >
    > > > On 7/20/05, Simon Brunning <> wrote:
    > > >> On 7/20/05, Mage <> wrote:
    > > >> > Or is there better way?
    > > >> >
    > > >> > for (i, url) in [(i,links) for i in range(len(links))]:
    > > >>
    > > >> for i, url in enumerate(links):
    > > >>
    > > >
    > > > +2 for creating seeing a need and crafting a reasonable solution, but
    > > > -1 for not reading the section on builtins to see if it existed
    > > > already.

    > >
    > > To see if *what* existed already?
    > >
    > > It is well and good to say RTFM, but there are 697 subsections to the
    > > Python Library reference, and if you don't know what you are looking for,
    > > and beginners rarely are, it isn't obvious which is the right section to
    > > read. And the Library Reference isn't even "the" manual: there is also the
    > > global module reference and language reference.
    > >
    > > If you already know what you are looking for, reading the manual is great
    > > advice. Browsing the manual looking for interesting tidbits can even be
    > > fun for a certain mindset. But if you don't know enough to know what to
    > > look for, where in the 2000-odd sections of the Python references will
    > > you find it?
    > >

    >
    > I said the *builtins* section. I think you learn pretty quick that
    > figuring out what functions are builtins is pretty important in every
    > language. There's a fair number of people out there giving the advice
    > to read chapter 2 of the library reference cover-to-cover for a good
    > starter on python.

    The quick reference guide is also a nice place to find all the most important
    information:
    http://rgruet.free.fr/PQR24/PQR2.4.html
    >
    > Furthermore, I wasn't being hard on the guy, he still added up to +1.
    > Lighten up, I was joking.
    >
    > >
    > >
    > > > (As for its pythonicity, I would have recommended isolating it into a
    > > > function and making it a generator:

    > >
    > > It is easy to take this to extremes. It isn't necessary to isolate
    > > everything into its own object, or class, or module. Too much
    > > encapsulation is just as bad as too little.
    > >

    >
    > agreed; his listcomp just looks awkward inside the for loop statement;
    > if it were my code, I would put it into a function. He asked if his
    > code was pythonic, and I think the (non-extreme) pythonic thing to do
    > would be to put his listcomp into a function.
    >
    > >
    > > > def my_enumerate(enumerable):
    > > > i = 0
    > > > for elt in enumerable:
    > > > yield (i, elt)
    > > > i += 1
    > > >
    > > > for i, url in my_enumerate(links):
    > > >
    > > > but it's not too bad as it is. Also, my function is completely
    > > > untested - it's close to right though.)

    > >
    > > What is the advantage of your function my_enumerate over the Python
    > > built-in enumerate?
    > >
    > >

    >
    > absolutely none; I just was saying how I would encapsulate it into a function.


    Except that enumerate was introduced in 2.3 so if you are working with an
    older version of Python it won't be there.
    >
    > Peace
    > Bill Mill
    > bill.mill at gmail.com
    > --
    > http://mail.python.org/mailman/listinfo/python-list
     
    Chris Lambacher, Jul 23, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kjockey

    pythonic malloc

    kjockey, Jul 21, 2003, in forum: Python
    Replies:
    7
    Views:
    307
    Brad Hards
    Jul 23, 2003
  2. Tom Evans
    Replies:
    9
    Views:
    321
    Tom Evans
    Oct 29, 2003
  3. zapazap
    Replies:
    2
    Views:
    406
    Mark Hammond
    Jan 26, 2004
  4. zapazap
    Replies:
    2
    Views:
    422
    zapazap
    Mar 2, 2004
  5. Carl J. Van Arsdall
    Replies:
    4
    Views:
    519
    Bruno Desthuilliers
    Feb 7, 2006
Loading...

Share This Page