Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: is this pythonic?

Reply
Thread Tools

Re: is this pythonic?

 
 
Bill Mill
Guest
Posts: n/a
 
      07-20-2005
On 7/20/05, Simon Brunning <(E-Mail Removed)> wrote:
> On 7/20/05, Mage <(E-Mail Removed)> wrote:
> > Or is there better way?
> >
> > for (i, url) in [(i,links[i]) 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
 
Reply With Quote
 
 
 
 
Bill Mill
Guest
Posts: n/a
 
      07-21-2005
On 7/21/05, Steven D'Aprano <(E-Mail Removed)> wrote:
> On Wed, 20 Jul 2005 16:30:10 -0400, Bill Mill wrote:
>
> > On 7/20/05, Simon Brunning <(E-Mail Removed)> wrote:
> >> On 7/20/05, Mage <(E-Mail Removed)> wrote:
> >> > Or is there better way?
> >> >
> >> > for (i, url) in [(i,links[i]) 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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-21-2005
On Wed, 20 Jul 2005 16:30:10 -0400, Bill Mill wrote:

> On 7/20/05, Simon Brunning <(E-Mail Removed)> wrote:
>> On 7/20/05, Mage <(E-Mail Removed)> wrote:
>> > Or is there better way?
>> >
>> > for (i, url) in [(i,links[i]) 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.

 
Reply With Quote
 
Michael Hoffman
Guest
Posts: n/a
 
      07-21-2005
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
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-21-2005
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.

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-21-2005
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.

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      07-21-2005

"Steven D'Aprano" <(E-Mail Removed)> wrote in message
newsan.2005.07.21.15.43.28.56459@REMOVETHIScyber .com.au...
> 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



 
Reply With Quote
 
Chris Lambacher
Guest
Posts: n/a
 
      07-23-2005
On Thu, Jul 21, 2005 at 10:27:24AM -0400, Bill Mill wrote:
> On 7/21/05, Steven D'Aprano <(E-Mail Removed)> wrote:
> > On Wed, 20 Jul 2005 16:30:10 -0400, Bill Mill wrote:
> >
> > > On 7/20/05, Simon Brunning <(E-Mail Removed)> wrote:
> > >> On 7/20/05, Mage <(E-Mail Removed)> wrote:
> > >> > Or is there better way?
> > >> >
> > >> > for (i, url) in [(i,links[i]) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments