Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #61195 > unrolled thread
| Started by | Dan Stromberg <drsalists@gmail.com> |
|---|---|
| First post | 2013-12-06 15:54 -0800 |
| Last post | 2013-12-10 00:59 +1100 |
| Articles | 7 — 6 participants |
Back to article view | Back to comp.lang.python
One liners Dan Stromberg <drsalists@gmail.com> - 2013-12-06 15:54 -0800
Re: One liners Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-07 02:07 +0000
Re: One liners Dan Stromberg <drsalists@gmail.com> - 2013-12-06 19:20 -0800
Re: One liners Roy Smith <roy@panix.com> - 2013-12-06 22:53 -0500
Re: One liners Steven D'Aprano <steve@pearwood.info> - 2013-12-10 03:36 +0000
Re: One liners Neil Cerutti <neilc@norwich.edu> - 2013-12-09 13:49 +0000
Re: One liners Chris Angelico <rosuav@gmail.com> - 2013-12-10 00:59 +1100
| From | Dan Stromberg <drsalists@gmail.com> |
|---|---|
| Date | 2013-12-06 15:54 -0800 |
| Subject | One liners |
| Message-ID | <mailman.3674.1386374070.18130.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
Does anyone else feel like Python is being dragged too far in the direction of long, complex, multiline one-liners? Or avoiding temporary variables with descriptive names? Or using regex's for everything under the sun? What happened to using classes? What happened to the beautiful emphasis on readability? What happened to debuggability (which is always harder than writing things in the first place)? And what happened to string methods? I'm pleased to see Python getting more popular, but it feels like a lot of newcomers are trying their best to turn Python into Perl or something, culturally speaking.
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-12-07 02:07 +0000 |
| Message-ID | <52a282d1$0$30003$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #61195 |
On Fri, 06 Dec 2013 15:54:22 -0800, Dan Stromberg wrote:
> Does anyone else feel like Python is being dragged too far in the
> direction of long, complex, multiline one-liners? Or avoiding temporary
> variables with descriptive names? Or using regex's for everything under
> the sun?
All those things are stylistic issues, not language issues. Yes, I see
far too many people trying to squeeze three lines of code into one, but
that's their choice, not the language leading them that way.
On the other hand, Python code style is influenced strongly by functional
languages like Lisp, Scheme and Haskell (despite the radically different
syntax). Python has even been described approvingly as "Lisp without the
brackets". To somebody coming from a C or Pascal procedural background,
or a Java OOP background, such functional-style code might seem too
concise and/or weird. But frankly, I think that such programmers would
write better code with a more functional approach. I refuse to apologise
for writing the one-liner:
result = [func(item) for item in sequence]
instead of four:
result = []
for i in range(len(sequence)):
item = sequence[i]
result.append(func(item))
> What happened to using classes? What happened to the beautiful emphasis
> on readability? What happened to debuggability (which is always harder
> than writing things in the first place)? And what happened to string
> methods?
What about string methods?
As far as classes go, I find that they're nearly always overkill. Most of
the time, a handful of pre-written standard classes, like dict, list,
namedtuple and the like, get me 90% of the way to where I need to go.
The beauty of Python is that it is a multi-paradigm language. You can
write imperative, procedural, functional, OOP, or pipelining style (and
probably more). The bad thing about Python is that if you're reading
other people's code you *need* to be familiar with all those styles.
> I'm pleased to see Python getting more popular, but it feels like a lot
> of newcomers are trying their best to turn Python into Perl or
> something, culturally speaking.
They're probably writing code using the idioms they are used to from
whatever language they have come from. Newcomers nearly always do this.
The more newcomers you get, the less Pythonic the code you're going to
see from them.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Dan Stromberg <drsalists@gmail.com> |
|---|---|
| Date | 2013-12-06 19:20 -0800 |
| Message-ID | <mailman.3695.1386386415.18130.python-list@python.org> |
| In reply to | #61215 |
[Multipart message — attachments visible in raw view] — view raw
On Fri, Dec 6, 2013 at 6:07 PM, Steven D'Aprano < steve+comp.lang.python@pearwood.info> wrote: > On Fri, 06 Dec 2013 15:54:22 -0800, Dan Stromberg wrote: > > > Does anyone else feel like Python is being dragged too far in the > > direction of long, complex, multiline one-liners? Or avoiding temporary > > variables with descriptive names? Or using regex's for everything under > > the sun? > > All those things are stylistic issues, not language issues. Yes, I see > far too many people trying to squeeze three lines of code into one, but > that's their choice, not the language leading them that way. > Yes, stylistic, or even "cultural". > I refuse to apologise > for writing the one-liner: > > result = [func(item) for item in sequence] > > instead of four: > > result = [] > for i in range(len(sequence)): > item = sequence[i] > result.append(func(item)) > IMO, this is a time when the one liner is more clear. But if you start trying to stretch that to extremes, it becomes worse instead of better. > > > What happened to using classes? What happened to the beautiful emphasis > > on readability? What happened to debuggability (which is always harder > > than writing things in the first place)? And what happened to string > > methods? > > What about string methods? > A lot of things people do with regex's, could be done with string methods more clearly and concisely. The beauty of Python is that it is a multi-paradigm language. You can > write imperative, procedural, functional, OOP, or pipelining style (and > probably more). The bad thing about Python is that if you're reading > other people's code you *need* to be familiar with all those styles. > That's fine. That's appropriate. But I imagine any of these can be done with the intention of being more clever than clear. BTW, what's pipelining style? Like bash? > I'm pleased to see Python getting more popular, but it feels like a lot > of newcomers are trying their best to turn Python into Perl or > something, culturally speaking. They're probably writing code using the idioms they are used to from > whatever language they have come from. Newcomers nearly always do this. > The more newcomers you get, the less Pythonic the code you're going to > see from them. > Nod.
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-12-06 22:53 -0500 |
| Message-ID | <roy-6E31DC.22530606122013@news.panix.com> |
| In reply to | #61225 |
In article <mailman.3695.1386386415.18130.python-list@python.org>, Dan Stromberg <drsalists@gmail.com> wrote: > A lot of things people do with regex's, could be done with string methods > more clearly and concisely. That is true. The problem is, there are a lot of things for which regex is the right tool, but people get out of practice using them (or never learned how) because they gravitate to string methods for most tasks. It's like any sharp tool. When skillfully handled, they're excellent at the tasks they were designed for. But, if you don't practice the necessary skills, you end up just re-enacting the Black Night Sketch.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2013-12-10 03:36 +0000 |
| Message-ID | <52a68c59$0$2829$c3e8da3$76491128@news.astraweb.com> |
| In reply to | #61225 |
On Fri, 06 Dec 2013 19:20:07 -0800, Dan Stromberg wrote: > On Fri, Dec 6, 2013 at 6:07 PM, Steven D'Aprano < > steve+comp.lang.python@pearwood.info> wrote: >> The beauty of Python is that it is a multi-paradigm language. You can >> write imperative, procedural, functional, OOP, or pipelining style (and >> probably more). The bad thing about Python is that if you're reading >> other people's code you *need* to be familiar with all those styles. >> >> > That's fine. That's appropriate. But I imagine any of these can be > done with the intention of being more clever than clear. > > BTW, what's pipelining style? Like bash? Yes, correct. You have a data stream and you pass it through various filters to process it. David Beazley has some nice examples of writing pipelining code: http://www.dabeaz.com/generators/index.html -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2013-12-09 13:49 +0000 |
| Message-ID | <mailman.3778.1386596990.18130.python-list@python.org> |
| In reply to | #61215 |
On 2013-12-07, Dan Stromberg <drsalists@gmail.com> wrote:
> BTW, what's pipelining style? Like bash?
I think, in Python, it might refer to composing your program of
a series of generators.
names = (p.name for p in db.query_people() if p.total_purchases > 0)
names = (n.upper() for n in names)
names = (n for n in names if not n.startswith("Q"))
for n in names:
# Finally actually do something.
Coincidentally it's a nice way to break up an ugly one-liner. I
also really like that if I choose to no longer filter out
customers whose name begins with Q, I can just comment out that
one line. Try doing that with the one-liner version!
>> I'm pleased to see Python getting more popular, but it feels
>> like a lot of newcomers are trying their best to turn Python
>> into Perl or something, culturally speaking.
>
>> They're probably writing code using the idioms they are used
>> to from whatever language they have come from. Newcomers
>> nearly always do this. The more newcomers you get, the less
>> Pythonic the code you're going to see from them.
>
> Nod.
That's the sound of practicality slapping purity with a fish. A
new Python programmer can generally just get her code working in
a fairly comfortable way, then possibly rewrite it once her first
few programs become horrifying years later.
I haven't found time to rewrite all of mine yet. I still have a
program I use almost every day with an __init__ that returns
invalid objects but helpfully sets self.valid to 0.
--
Neil Cerutti
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-12-10 00:59 +1100 |
| Message-ID | <mailman.3779.1386597567.18130.python-list@python.org> |
| In reply to | #61215 |
On Tue, Dec 10, 2013 at 12:49 AM, Neil Cerutti <neilc@norwich.edu> wrote:
> names = (p.name for p in db.query_people() if p.total_purchases > 0)
> names = (n.upper() for n in names)
> names = (n for n in names if not n.startswith("Q"))
> for n in names:
> # Finally actually do something.
>
> Coincidentally it's a nice way to break up an ugly one-liner. I
> also really like that if I choose to no longer filter out
> customers whose name begins with Q, I can just comment out that
> one line. Try doing that with the one-liner version!
# With the restriction:
for n in (p.name.upper() for n in names if not n.startswith("Q")):
# Without the restriction:
for n in (p.name.upper() for n in names ):# if not n.startswith("Q")):
You just have to use the special "comment-out-partially" operator,
which looks like "):#" and is inspired by some kind of insane smiley
with weird hair.
ChrisA
(Anyone got the cheek de-tonguer handy?)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web