Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50142 > unrolled thread
| Started by | Joshua Landau <joshua.landau.ws@gmail.com> |
|---|---|
| First post | 2013-07-08 11:39 +0100 |
| Last post | 2013-07-08 14:07 +0100 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: A small question about PEP 8 Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-08 11:39 +0100
Re: A small question about PEP 8 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-08 12:02 +0000
Re: A small question about PEP 8 Dave Angel <davea@davea.name> - 2013-07-08 08:33 -0400
Re: A small question about PEP 8 Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-08 14:07 +0100
| From | Joshua Landau <joshua.landau.ws@gmail.com> |
|---|---|
| Date | 2013-07-08 11:39 +0100 |
| Subject | Re: A small question about PEP 8 |
| Message-ID | <mailman.4380.1373280314.3114.python-list@python.org> |
On 8 July 2013 00:32, Xue Fuqiao <xfq.free@gmail.com> wrote:
> Hi all,
>
> (English is not my native language; please excuse typing errors.)
>
> I'm a Python newbie and just started reading PEP 8. PEP says:
>
> -----------------------------------------------------------------------
> |The closing brace/bracket/parenthesis on multi-line constructs may
> |either line up under the last item of the list, as in:
> |
> |my_list = [
> | 1, 2, 3,
> | 4, 5, 6,
> | ]
> |result = some_function_that_takes_arguments(
> | 'a', 'b', 'c',
> | 'd', 'e', 'f',
> | )
> -----------------------------------------------------------------------
>
> I think the last item in my_list/result is 6/'f', respectively. So why
> doesn't the bracket/paren line up _under_ the last item? ISTM the code
> isn't consistent with the description.
>
> I have searched the archive of c.l.p and the web, but nothing helped.
> Can anyone point me in the right direction?
You will grow to be a wonderful pedant. What it means is that the
indentation will match the last one. Imagine:
"""
a_wonderful_set_of_things = {
bannanas_made_of_apples,
chocolate_covered_horns,
doors_that_slide,
china_but_on_the_moon,
buffalo_with_windy_hair,
not_missing_an_end_brace
"""¹
Now, there are several places you can put the end brace. You can (be a
massive fool and) put it after the last item:
"""
a_wonderful_set_of_things = {
...,
not_missing_an_end_brace}
"""
You can also (be a fool and) put it at the same *indentation*:
"""
a_wonderful_set_of_things = {
...,
not_missing_an_end_brace
}
"""
Or you can (be sane) and put it at no indentation:
"""
a_wonderful_set_of_things = {
...,
not_missing_an_end_brace
}
"""
Theoretically, there are more places you could put it (but we won't go
there... *shudder*).
The second of these is the one that PEP 8 was trying to explain. I
agree wording could be improved, but hey. You can file a bug report at
bugs.python.org if you care enough.
¹}
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-07-08 12:02 +0000 |
| Message-ID | <51daaa51$0$9505$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #50142 |
On Mon, 08 Jul 2013 11:39:21 +0100, Joshua Landau wrote:
> On 8 July 2013 00:32, Xue Fuqiao <xfq.free@gmail.com> wrote:
>> Hi all,
>>
>> (English is not my native language; please excuse typing errors.)
>>
>> I'm a Python newbie and just started reading PEP 8. PEP says:
>>
>> -----------------------------------------------------------------------
>> |The closing brace/bracket/parenthesis on multi-line constructs may
>> |either line up under the last item of the list, as in: |
>> |my_list = [
>> | 1, 2, 3,
>> | 4, 5, 6,
>> | ]
>> |result = some_function_that_takes_arguments(
>> | 'a', 'b', 'c',
>> | 'd', 'e', 'f',
>> | )
>> -----------------------------------------------------------------------
>>
>> I think the last item in my_list/result is 6/'f', respectively. So why
>> doesn't the bracket/paren line up _under_ the last item? ISTM the code
>> isn't consistent with the description.
>>
>> I have searched the archive of c.l.p and the web, but nothing helped.
>> Can anyone point me in the right direction?
>
> You will grow to be a wonderful pedant. What it means is that the
> indentation will match the last one.
That's obvious from the example given, but that's nothing like the
description given.
Imagine:
>
> """
> a_wonderful_set_of_things = {
> bannanas_made_of_apples,
> chocolate_covered_horns,
> doors_that_slide,
> china_but_on_the_moon,
> buffalo_with_windy_hair,
> not_missing_an_end_brace
> """¹
>
> Now, there are several places you can put the end brace. You can (be a
> massive fool and) put it after the last item:
>
> """
> a_wonderful_set_of_things = {
> ...,
> not_missing_an_end_brace}
> """
Well, call me a fool then, because when I have code that extends over
*one* additional line, I prefer that:
raise ValueError(
"Some error message too long to fit on the above line")
rather than:
raise ValueError(
"Some error message too long to fit on the above line"
)
> You can also (be a fool and) put it at the same *indentation*:
>
> """
> a_wonderful_set_of_things = {
> ...,
> not_missing_an_end_brace
> }
> """
Call me a fool again, since I prefer this when there are more than one
additional line. At least this time I'm in good company, since that's
recommended by PEP 8.
> Or you can (be sane) and put it at no indentation:
>
> """
> a_wonderful_set_of_things = {
> ...,
> not_missing_an_end_brace
> }
> """
I consider that the least aesthetically pleasing, and also rather awkward:
some_result = some_function(
arg1, arg2, arg3, arg4,
[item1, item2, item3, item4,
item5, item6, item7,
item8, item9, item10,
],
arg6, key=spam, word=eggs,
extras=None, foo=bar,
)
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-07-08 08:33 -0400 |
| Message-ID | <mailman.4383.1373286831.3114.python-list@python.org> |
| In reply to | #50148 |
On 07/08/2013 08:02 AM, Steven D'Aprano wrote:
> On Mon, 08 Jul 2013 11:39:21 +0100, Joshua Landau wrote:
>
<SNIP>
>
>> Or you can (be sane) and put it at no indentation:
>>
>> """
>> a_wonderful_set_of_things = {
>> ...,
>> not_missing_an_end_brace
>> }
>> """
>
> I consider that the least aesthetically pleasing, and also rather awkward:
>
>
> some_result = some_function(
> arg1, arg2, arg3, arg4,
> [item1, item2, item3, item4,
> item5, item6, item7,
> item8, item9, item10,
> ],
> arg6, key=spam, word=eggs,
> extras=None, foo=bar,
> )
or even better:
class MyClass:
def my_method(self):
if self.flag:
self.value.append(
arg1,
arg2,
arg3
)
return self.value
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Joshua Landau <joshua.landau.ws@gmail.com> |
|---|---|
| Date | 2013-07-08 14:07 +0100 |
| Message-ID | <mailman.4386.1373288900.3114.python-list@python.org> |
| In reply to | #50148 |
On 8 July 2013 13:02, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Mon, 08 Jul 2013 11:39:21 +0100, Joshua Landau wrote:
> Imagine:
>>
>> """
>> a_wonderful_set_of_things = {
>> bannanas_made_of_apples,
>> chocolate_covered_horns,
>> doors_that_slide,
>> china_but_on_the_moon,
>> buffalo_with_windy_hair,
>> not_missing_an_end_brace
>> """¹
>>
>> Now, there are several places you can put the end brace. You can (be a
>> massive fool and) put it after the last item:
>>
>> """
>> a_wonderful_set_of_things = {
>> ...,
>> not_missing_an_end_brace}
>> """
>
> Well, call me a fool then, because when I have code that extends over
> *one* additional line, I prefer that:
>
> raise ValueError(
> "Some error message too long to fit on the above line")
>
> rather than:
>
> raise ValueError(
> "Some error message too long to fit on the above line"
> )
Fool.
>> You can also (be a fool and) put it at the same *indentation*:
>>
>> """
>> a_wonderful_set_of_things = {
>> ...,
>> not_missing_an_end_brace
>> }
>> """
>
> Call me a fool again, since I prefer this when there are more than one
> additional line. At least this time I'm in good company, since that's
> recommended by PEP 8.
Fool (less so, though).
>> Or you can (be sane) and put it at no indentation:
>>
>> """
>> a_wonderful_set_of_things = {
>> ...,
>> not_missing_an_end_brace
>> }
>> """
>
> I consider that the least aesthetically pleasing, and also rather awkward:
>
>
> some_result = some_function(
> arg1, arg2, arg3, arg4,
> [item1, item2, item3, item4,
> item5, item6, item7,
> item8, item9, item10,
> ],
> arg6, key=spam, word=eggs,
> extras=None, foo=bar,
> )
http://xkcd.com/605/
It was obvious¹ I was talking in context of my example. I used "no
indentation" because in that case "no indentation" was correct. If you
indent line #1, it follows that other lines might well be indented to
keep style consistent.
¹ I hope. I never suggested otherwise.
Since it's not too clear for you, my usage of fool was meant
pseudoironically as a subtle reminder that it's all subjective anyway,
but that I'm also still right.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web