Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Dev

PEP 8 modernisation

 

 

First page Previous page 1 2 Next page Last page  View All Python dev RSS feed   Index | Next | Previous | View Threaded


brian at python

Aug 1, 2013, 1:44 PM

Post #26 of 36 (70 views)
Permalink
Re: PEP 8 modernisation [In reply to]

On Thu, Aug 1, 2013 at 3:36 PM, Terry Reedy <tjreedy [at] udel> wrote:
> On 8/1/2013 11:03 AM, Alexander Shorin wrote:
>>
>> ...and, if so, why lambda's?(: Without backward compatibility point I
>> see that they are getting "unofficially" deprecated and their usage is
>> dishonoured.
>
>
> Please stop both the top-posting and the FUD.

Top posting doesn't matter.

The end.
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


alexander.belopolsky at gmail

Aug 1, 2013, 1:52 PM

Post #27 of 36 (70 views)
Permalink
Re: PEP 8 modernisation [In reply to]

On Thu, Aug 1, 2013 at 4:29 PM, Terry Reedy <tjreedy [at] udel> wrote:

> def f(x): return 2*x
> f = lambda x: 2*x
>

Am I the only one who finds the second line above much more readable than
the first? The def statement is not intended to be written in one line.
The readability suffers because the argument is separated from the value
expression by return keyword.

When def statement is written traditionally:

def f(x):
return 2*x

It is easy to run the eyes over the right margin and recognize a function
that in a math paper would be written as "f: x -> 2 x". Same is true about
lambda expression. While C# syntax "f = (x => 2*x)" is probably closest to
mathematical notation, "f = lambda x: 2*x" is close enough. One can
mentally focus on the "x: 2*x" part and ignore the rest.


tjreedy at udel

Aug 1, 2013, 1:56 PM

Post #28 of 36 (70 views)
Permalink
Re: PEP 8 modernisation [In reply to]

On 8/1/2013 11:35 AM, Alexander Belopolsky wrote:

> Here is one use-case where .. = lambda .. cannot be replaced with def ..
>
> op['add'] = lambda x,y: x+y
> op['mul'] = lambda x, y: x*y

Yes, you are binding the functions to named slots, not to names, so not
covered by the PEP. Once might still want to replace the expressions
themselves, at the cost of more typing, for the advantage of better
representations.

op = { 'add': lambda x,y: x*y, 'mul': lambda x, y: x+y}
print(op) # no apparent problem
# {'add': <function <lambda> at 0x000000000227F730>,
# 'mul': <function <lambda> at 0x00000000033867B8>}

def add(x, y): return x + y
def mul(x, y): return x * y
# These can be unittested individually

op = {'add': mul, 'mul': add} # mistake easily seen in original code
print(op)
# {'add': <function mul at 0x0000000003440950>,
# 'mul': <function add at 0x00000000034408C8>}
# problem apparent to user who import this object and prints it when
code fails

If op has 20 such functions, names become even more of an advantage

--
Terry Jan Reedy

_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


brian at python

Aug 1, 2013, 1:57 PM

Post #29 of 36 (70 views)
Permalink
Re: PEP 8 modernisation [In reply to]

On Thu, Aug 1, 2013 at 3:44 PM, Brian Curtin <brian [at] python> wrote:
> On Thu, Aug 1, 2013 at 3:36 PM, Terry Reedy <tjreedy [at] udel> wrote:
>> On 8/1/2013 11:03 AM, Alexander Shorin wrote:
>>>
>>> ...and, if so, why lambda's?(: Without backward compatibility point I
>>> see that they are getting "unofficially" deprecated and their usage is
>>> dishonoured.
>>
>>
>> Please stop both the top-posting and the FUD.
>
> Top posting doesn't matter.
>
> The end.

Actually, quick expansion on this before moving along: if you're going
to call someone out for top posting, you can't ignore the many high
profile people who do it every time and single out the newcomer.
That's why I said something.

Sorry for the OT.
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


ncoghlan at gmail

Aug 1, 2013, 2:48 PM

Post #30 of 36 (70 views)
Permalink
Re: PEP 8 modernisation [In reply to]

On 2 Aug 2013 01:18, "Alexander Belopolsky" <alexander.belopolsky [at] gmail>
wrote:
>
>
> On Thu, Aug 1, 2013 at 10:21 AM, R. David Murray <rdmurray [at] bitdance>
wrote:
>>
>> > I'm guessing it's short enough you can say you tried, but long
>> > enough to annoy traditionalists anyway.
>> >
>> > I'm annoyed already. :-)
>>
>> +1 :)
>
>
> +1 :)
>
> I recently gave up and reset default auto-wrap margin to 120 locally.
This change had little effect on code because most line breaks in code are
inserted manually anyways. However, docstrings are beginning to suffer.
The "short description" line is not that short anymore and multi-paragraph
prose filled between 4- and 120-characters margin is hard to read.
>
> I will start experimenting with 100-char limit, but I think it is still
too wide for auto-wrapped text. Maybe we should have a stronger
recommendation to keep 80-char limit for docstrings and other embedded
text. It is OK to have an occasional long line in code, but readability
suffers when you have every line close to 100 chars.

1. The recommended length limit for flowed text is still *72* (since it
doesn't have the structural constraints code does).
2. The preferred length limit for code is still 79.

The "up to 99 if it improves readability" escape clause was added because
Guido deemed the occasional long line a lesser evil than the contortions he
has seen people apply to code to stay within the 79 character limit (most
notably, using cryptic variable names because they're shorter).

That entire section of the PEP was completely rewritten - we didn't just
s/79/99/ with the old content.

Cheers,
Nick.

>
> Another observation is that long lines in code are usually heavily
indented. This makes them still readable because non-white characters
still fit within the field of view. Again, this is not the case for
docstrings, comments or other embedded prose.
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev [at] python
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
http://mail.python.org/mailman/options/python-dev/ncoghlan%40gmail.com
>


kxepal at gmail

Aug 2, 2013, 12:28 AM

Post #31 of 36 (70 views)
Permalink
Re: PEP 8 modernisation [In reply to]

Hi Terry,

On Fri, Aug 2, 2013 at 12:29 AM, Terry Reedy <tjreedy [at] udel> wrote:
> def f(x): return 2*x
> f = lambda x: 2*x
> Three spaces is seldom a crucial difference. If the expression is so long it go past the limit (whatever we decide it is), it can be wrapped.

and if I have multiple lambda-like def`s it will hit the PEP rule :

> While sometimes it's okay to put an if/for/while with a small body on the same line, never do this for multi-clause statements. Also avoid folding such long lines!

On Fri, Aug 2, 2013 at 12:29 AM, Terry Reedy <tjreedy [at] udel> wrote:
>> and/or to remove duplicates (especially for sorted groupby case)?
> I do not understand this.

See [Python-Dev] Lambda [was Re: PEP 8 modernisation] thread for example:
http://mail.python.org/pipermail/python-dev/2013-August/127715.html

On Fri, Aug 2, 2013 at 12:35 AM, Terry Reedy <tjreedy [at] udel> wrote:
>> I understand this, but I'm a bit confused about fate of lambdas with
>> such guideline since I see no more reasons to use them with p.9
>> statement: long lines, code duplicate, no mock and well tests etc. -
>> all these problems could be solved with assigning lambda to some name,
>> but now they are looks useless (or useful only for very trivial cases)
>
>I do not understand most of that, but...
>The guideline is not meant to cover passing a function by parameter name. mylist.sort(key=lambda x: x[0]) is still ok. Does "Always use a def statement >instead of assigning a lambda expression to a name." need 'in an assignment statement' added?

I wrote about that lambda`s use case become too small to use them in
real code. If they are dishonoured - need to write so and clearly, but
not limiting their use cases step by step till every Python devs will
think like "Lambdas? Why? Remove them!".

Using `dict` to store lambdas:

> op = { 'add': lambda x,y: x*y, 'mul': lambda x, y: x+y}

Shows the hack to bypass PEP8 guides. Do you like to see code above instead of:

add = lambda x,y: x*y
mul = lambda x, y: x+y

Probably, I don't since dict is a blackbox and I have to check things
first before use them.

Disclaimer: I don't try to stand for lambdas, I'm not using them
everywhere in my code, but I'd like to know answer for the question
"Why lambdas?". Currently, it is "Handy shorthand functions - use them
free", but with new PEP-8 statement I really have to think like
"Lambdas? Really, why?".

P.S.

On Thu, Aug 1, 2013 at 3:36 PM, Terry Reedy <tjreedy [at] udel> wrote:
> Please stop both the top-posting and the FUD.

Sorry, different ML, different rules. You know mail client with allows
to have per-address reply setting? I don't, but would like to see your
suggestions in private answer. Thanks.
--
,,,^..^,,,


On Fri, Aug 2, 2013 at 12:56 AM, Terry Reedy <tjreedy [at] udel> wrote:
> On 8/1/2013 11:35 AM, Alexander Belopolsky wrote:
>
>> Here is one use-case where .. = lambda .. cannot be replaced with def ..
>>
>> op['add'] = lambda x,y: x+y
>> op['mul'] = lambda x, y: x*y
>
>
> Yes, you are binding the functions to named slots, not to names, so not
> covered by the PEP. Once might still want to replace the expressions
> themselves, at the cost of more typing, for the advantage of better
> representations.
>
> op = { 'add': lambda x,y: x*y, 'mul': lambda x, y: x+y}
> print(op) # no apparent problem
> # {'add': <function <lambda> at 0x000000000227F730>,
> # 'mul': <function <lambda> at 0x00000000033867B8>}
>
> def add(x, y): return x + y
> def mul(x, y): return x * y
> # These can be unittested individually
>
> op = {'add': mul, 'mul': add} # mistake easily seen in original code
> print(op)
> # {'add': <function mul at 0x0000000003440950>,
> # 'mul': <function add at 0x00000000034408C8>}
> # problem apparent to user who import this object and prints it when code
> fails
>
> If op has 20 such functions, names become even more of an advantage
>
> --
> Terry Jan Reedy
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev [at] python
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/kxepal%40gmail.com
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


ncoghlan at gmail

Aug 2, 2013, 2:10 AM

Post #32 of 36 (70 views)
Permalink
Re: PEP 8 modernisation [In reply to]

On 2 Aug 2013 17:31, "Alexander Shorin" <kxepal [at] gmail> wrote:
>
> Hi Terry,
>
> On Fri, Aug 2, 2013 at 12:29 AM, Terry Reedy <tjreedy [at] udel> wrote:
> > def f(x): return 2*x
> > f = lambda x: 2*x
> > Three spaces is seldom a crucial difference. If the expression is so
long it go past the limit (whatever we decide it is), it can be wrapped.
>
> and if I have multiple lambda-like def`s it will hit the PEP rule :
>
> > While sometimes it's okay to put an if/for/while with a small body on
the same line, never do this for multi-clause statements. Also avoid
folding such long lines!
>
> On Fri, Aug 2, 2013 at 12:29 AM, Terry Reedy <tjreedy [at] udel> wrote:
> >> and/or to remove duplicates (especially for sorted groupby case)?
> > I do not understand this.
>
> See [Python-Dev] Lambda [was Re: PEP 8 modernisation] thread for example:
> http://mail.python.org/pipermail/python-dev/2013-August/127715.html
>
> On Fri, Aug 2, 2013 at 12:35 AM, Terry Reedy <tjreedy [at] udel> wrote:
> >> I understand this, but I'm a bit confused about fate of lambdas with
> >> such guideline since I see no more reasons to use them with p.9
> >> statement: long lines, code duplicate, no mock and well tests etc. -
> >> all these problems could be solved with assigning lambda to some name,
> >> but now they are looks useless (or useful only for very trivial cases)
> >
> >I do not understand most of that, but...
> >The guideline is not meant to cover passing a function by parameter
name. mylist.sort(key=lambda x: x[0]) is still ok. Does "Always use a def
statement >instead of assigning a lambda expression to a name." need 'in an
assignment statement' added?
>
> I wrote about that lambda`s use case become too small to use them in
> real code. If they are dishonoured - need to write so and clearly, but
> not limiting their use cases step by step till every Python devs will
> think like "Lambdas? Why? Remove them!".

Lambda was almost removed in Python 3.

>
> Using `dict` to store lambdas:
>
> > op = { 'add': lambda x,y: x*y, 'mul': lambda x, y: x+y}
>
> Shows the hack to bypass PEP8 guides. Do you like to see code above
instead of:
>
> add = lambda x,y: x*y
> mul = lambda x, y: x+y
>
> Probably, I don't since dict is a blackbox and I have to check things
> first before use them.

People are free to write their own style guides that disagree with pep 8 (a
point which is now made explicitly in the PEP).

>
> Disclaimer: I don't try to stand for lambdas, I'm not using them
> everywhere in my code, but I'd like to know answer for the question
> "Why lambdas?". Currently, it is "Handy shorthand functions - use them
> free", but with new PEP-8 statement I really have to think like
> "Lambdas? Really, why?".

Use them for an anonymous function as an expression. All PEP 8 is now
saying is that giving a lambda a name is to completely misunderstand what
they're for.

Cheers,
Nick.


kxepal at gmail

Aug 2, 2013, 4:02 AM

Post #33 of 36 (70 views)
Permalink
Re: PEP 8 modernisation [In reply to]

On Fri, Aug 2, 2013 at 1:10 PM, Nick Coghlan <ncoghlan [at] gmail> wrote:
> Lambda was almost removed in Python 3.
>
>>
>> Using `dict` to store lambdas:
>>
>> > op = { 'add': lambda x,y: x*y, 'mul': lambda x, y: x+y}
>>
>> Shows the hack to bypass PEP8 guides. Do you like to see code above
>> instead of:
>>
>> add = lambda x,y: x*y
>> mul = lambda x, y: x+y
>>
>> Probably, I don't since dict is a blackbox and I have to check things
>> first before use them.
>
> People are free to write their own style guides that disagree with pep 8 (a
> point which is now made explicitly in the PEP).
>
>>
>> Disclaimer: I don't try to stand for lambdas, I'm not using them
>> everywhere in my code, but I'd like to know answer for the question
>> "Why lambdas?". Currently, it is "Handy shorthand functions - use them
>> free", but with new PEP-8 statement I really have to think like
>> "Lambdas? Really, why?".
>
> Use them for an anonymous function as an expression. All PEP 8 is now saying
> is that giving a lambda a name is to completely misunderstand what they're
> for.
>
> Cheers,
> Nick.

Thanks for explanations, Nick, I'd got the point.

--
,,,^..^,,,
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


alexander.belopolsky at gmail

Aug 3, 2013, 6:30 PM

Post #34 of 36 (59 views)
Permalink
Re: PEP 8 modernisation [In reply to]

On Thu, Aug 1, 2013 at 8:44 AM, Nick Coghlan <ncoghlan [at] gmail> wrote:
>
> 9. Explicit guideline not to assign lambdas to names (use def, that's
> what it's for)


Would you consider changing the formatting in the recommended example from

def f(x): return 2*x

to

def f(x):
return 2*x

?

What is the modern view on single-line def? The "Other Recommendations"
section allows but discourages single-line if/for/while, but is silent
about def.


ncoghlan at gmail

Aug 3, 2013, 7:03 PM

Post #35 of 36 (59 views)
Permalink
Re: PEP 8 modernisation [In reply to]

On 4 Aug 2013 11:30, "Alexander Belopolsky" <alexander.belopolsky [at] gmail>
wrote:
>
>
> On Thu, Aug 1, 2013 at 8:44 AM, Nick Coghlan <ncoghlan [at] gmail> wrote:
> >
> > 9. Explicit guideline not to assign lambdas to names (use def, that's
> > what it's for)
>
>
> Would you consider changing the formatting in the recommended example from
>
> def f(x): return 2*x
>
> to
>
> def f(x):
> return 2*x
>
> ?

I consider a single line def acceptable when replacing an equivalent
lambda. Restricting it to a single line makes it solely about the spelling
of the assignment operation, without any vertical whitespace considerations.

Cheers,
Nick.

>
> What is the modern view on single-line def? The "Other Recommendations"
section allows but discourages single-line if/for/while, but is silent
about def.


steve at pearwood

Aug 3, 2013, 7:44 PM

Post #36 of 36 (59 views)
Permalink
Re: PEP 8 modernisation [In reply to]

On 02/08/13 06:52, Alexander Belopolsky wrote:
> On Thu, Aug 1, 2013 at 4:29 PM, Terry Reedy <tjreedy [at] udel> wrote:
>
>> def f(x): return 2*x
>> f = lambda x: 2*x
>>
>
> Am I the only one who finds the second line above much more readable than
> the first? The def statement is not intended to be written in one line.
> The readability suffers because the argument is separated from the value
> expression by return keyword.

You are not the only one.

I will continue to write "f = lambda ..." at the interactive interpreter without shame, although I rarely (never?) use it in code.


--
Steven
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com

First page Previous page 1 2 Next page Last page  View All Python dev RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.