Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32274

Re: attaching names to subexpressions

Path csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'example:': 0.03; 'syntax': 0.03; 'elif': 0.04; 'none:': 0.05; 'subject:skip:s 10': 0.05; 'exit': 0.07; 'line:': 0.07; 'lines.': 0.07; 'python': 0.09; 'python:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'splitting': 0.09; 'through.': 0.09; 'violates': 0.09; 'thread': 0.11; 'steve': 0.13; 'cases': 0.15; 'folks': 0.15; '"="': 0.16; '"while': 0.16; "'except'": 0.16; 'boolean': 0.16; 'construct.': 0.16; 'expressions?': 0.16; 'iterates': 0.16; 'line)': 0.16; 'loops': 0.16; 'outcomes': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'rewritten': 0.16; 'true:': 0.16; 'wrote:': 0.17; "shouldn't": 0.17; 'proposed': 0.20; 'beauty': 0.22; 'example': 0.23; 'somebody': 0.23; 'split': 0.23; 'statement': 0.23; 'this:': 0.23; 'idea': 0.24; 'feature': 0.24; 'pass': 0.25; 'tried': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'wondering': 0.26; '(e.g.': 0.27; 'am,': 0.27; 'question': 0.27; 'header:X-Complaints-To:1': 0.28; 'lines': 0.28; 'block,': 0.29; 'high.': 0.29; 'obliged': 0.29; 'statements': 0.29; "i'm": 0.29; "skip:' 10": 0.30; 'attach': 0.30; 'code': 0.31; 'skip:- 10': 0.32; 'could': 0.32; 'print': 0.32; 'problem': 0.33; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; 'clear': 0.35; 'problem,': 0.35; 'open': 0.35; 'doing': 0.35; 'sometimes': 0.35; 'something': 0.35; 'received:org': 0.36; 'really': 0.36; 'but': 0.36; 'useful': 0.36; 'possible': 0.37; 'two': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'further': 0.61; 'places': 0.61; 'first': 0.61; 'more': 0.63; 'bothered': 0.65; 'risk': 0.66; "'with'": 0.84; 'dry': 0.84; 'misses': 0.84; 'perspective.': 0.84; '"how': 0.91; 'leak': 0.91; 'convinced': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Gelonida N <gelonida@gmail.com>
Subject Re: attaching names to subexpressions
Date Sat, 27 Oct 2012 15:03:38 +0200
References <a533d812-480e-479b-9210-7097c2d70d73@a4g2000pbo.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host unicorn.dungeon.de
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1
In-Reply-To <a533d812-480e-479b-9210-7097c2d70d73@a4g2000pbo.googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2934.1351343030.27098.python-list@python.org> (permalink)
Lines 122
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351343030 news.xs4all.nl 6872 [2001:888:2000:d::a6]:42417
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32274

Show key headers only | View raw


On 10/27/2012 04:42 AM, Steve Howell wrote:
 > I have been reading the thread "while expression feature proposal,"
 > and one of the interesting outcomes of the thread is the idea that
 > Python could allow you to attach names to subexpressions, much like C
 > allows.  In C you can say something like this:
 >
 >    tax_next_year = (new_salary = salary * (1 + raise)) * tax_rate
 >
 > To avoid the "=" pitfall, folks have proposed something like this for
 > Python:
 >
 >    tax_next_year = ((salary * (1 + raise)) as new_salary) * tax_rate
 >    print new_salary, tax_next_year
 >
. . .
 >
 > If the problem statement is "How do I name subexpression?", then
 > Python already has a clear path--break your code up into multiple
 > lines.  I'm wondering where this simple solution really breaks down
 > from a readability perspective.  Perhaps with short-circuited boolean
 > expressions?
 >

For me two places where expression assignemts can be useful are while 
loops and sometimes elif statements.


While example
----------------

line = complex_exression
while line:
     do something with(line)
     line = complex_expression

violates clearly the DRY principle  and the risk to change one line but 
not the other is high.


The 'canonical way'
while True:
     line = complex_expression
     if not line:
         break
     do_something_with(line)

avoids this problem, but I was never really convinced about the
beauty / readbility of this construct.

One misses the exit condition of the while loop on the first glance.
In my opinion I shouldn't be obliged to read any of the indented lines
of the while statement on a first 'visual' pass through somebody elses 
code and still be able to see what the loop iterates through.


Following looks in my opinion nicer.

while complex_expression as line:
     do_something_with(line)

or the even more powerful suggestion:

while (complex_expression as line) is not None:
     do_something_with(line)


Elif Example:
--------------

value1 = expression1
if (value1):
    do_something
else:
     value2 = expression2
     if(value2):
         do_something_with(value2)
     else:
         value2 = expression3
         if(value3):
             do_something_with(value3)

Could be rewritten as

value1= expression1
if(value1):
     do_something_with(value1)
elif(expression2 as value2):
     do_something_with(value2)
elif(expression3 as value3):
     do_something_with(value3)



However in all other cases I really think using this new syntax would 
reduce readability andit would be better to just split the statement 
into two lines.

for while / elif statements splitting up is not possible without doing 
some further acrobatics, which render in my opinion the code less readable.


If the new syntax were adopted, then one open question
would be scoping.

value=expression
if(value):
     do_something_with(value)
elif(expression2 as value):
     do_something_with(value)
elif(expression3 as value):
     do_something_with(value)
print value # will value leak out of the elif statements or not

I never tried to use other 'as-variables' (e.g. from  'with' or 'except' 
statements) outside of their indented block,
so I never bothered to check how Python would react.





Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

attaching names to subexpressions Steve Howell <showell30@yahoo.com> - 2012-10-26 19:42 -0700
  Re: attaching names to subexpressions Gelonida N <gelonida@gmail.com> - 2012-10-27 15:03 +0200
    Re: attaching names to subexpressions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-28 00:49 +0000
      Re: attaching names to subexpressions rusi <rustompmody@gmail.com> - 2012-10-27 18:57 -0700
      Re: attaching names to subexpressions Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-10-28 01:57 -0400
        Re: attaching names to subexpressions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-28 06:59 +0000
          Re: attaching names to subexpressions Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-10-28 04:12 -0400
        Re: attaching names to subexpressions Neil Cerutti <neilc@norwich.edu> - 2012-10-29 11:53 +0000
      Re: attaching names to subexpressions Chris Angelico <rosuav@gmail.com> - 2012-10-28 17:01 +1100
      Re: attaching names to subexpressions "F.R." <anthra.norell@bluewin.ch> - 2012-10-28 08:12 +0100
      Re: attaching names to subexpressions Chris Angelico <rosuav@gmail.com> - 2012-10-28 21:44 +1100

csiph-web