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


Groups > comp.lang.python > #79685

Re: Status of side-effecting functions in python

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!bcyclone03.am1.xlned.com!bcyclone03.am1.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'programmer': 0.03; 'syntax': 0.04; 'value,': 0.04; 'correct.': 0.07; 'none,': 0.07; 'explanation': 0.09; 'function,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'thread': 0.14; 'changes': 0.15; "'w')": 0.16; '[none,': 0.16; "chris'": 0.16; 'expression.': 0.16; 'none.': 0.16; 'none]': 0.16; 'received:80.91.229.3': 0.16; 'received:mediaways.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:pool.mediaways.net': 0.16; 'silly': 0.16; 'statement.': 0.16; 'tool.': 0.16; 'written.': 0.16; 'subject:python': 0.16; 'sat,': 0.16; 'language': 0.16; 'wrote:': 0.18; 'pointed': 0.19; 'seems': 0.21; '>>>': 0.22; 'example': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; '(or': 0.24; 'question': 0.24; 'this:': 0.26; 'certain': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'appear': 0.29; 'chris': 0.29; 'external': 0.29; 'generally': 0.29; "doesn't": 0.30; 'characters': 0.30; 'moved': 0.30; 'statement': 0.30; 'strongly': 0.30; 'url:wiki': 0.31; '25,': 0.31; '>>>>': 0.31; "d'aprano": 0.31; 'extract': 0.31; 'pascal': 0.31; 'steven': 0.31; 'url:wikipedia': 0.31; 'void': 0.31; 'allows': 0.31; 'languages': 0.32; 'skip:m 30': 0.32; 'quite': 0.32; 'beginning': 0.33; 'sense': 0.34; 'core': 0.34; 'except': 0.35; 'something': 0.35; '(2)': 0.35; 'usual': 0.35; 'but': 0.35; 'there': 0.35; 'picking': 0.36; 'returning': 0.36; 'version:': 0.36; 'url:org': 0.36; 'should': 0.36; 'wrong': 0.37; 'too': 0.37; 'step': 0.37; 'skip:o 20': 0.38; '(3)': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'anything': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'easy': 0.60; 'future': 0.60; 'expression': 0.60; 'most': 0.60; 'numbers': 0.61; 'skip:n 10': 0.64; 'choose': 0.64; 'more': 0.64; 'teach': 0.65; 'here': 0.66; 'side': 0.67; 'results': 0.69; 'received:93': 0.72; '(ie': 0.84; 'cheating': 0.84; 'distinguish': 0.84; 'functions)': 0.84; 'not...': 0.84; 'side-effects': 0.84; 'technically': 0.84; 'state.': 0.95
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Subject Re: Status of side-effecting functions in python
Date Sat, 25 Oct 2014 23:41:52 +0200
References <c5e3a7e7-5997-47ee-8e81-759f2342c87a@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host frbg-5d84f74e.pool.mediaways.net
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0
In-Reply-To <c5e3a7e7-5997-47ee-8e81-759f2342c87a@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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15174.1414273325.18130.python-list@python.org> (permalink)
Lines 94
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1414273325 news.xs4all.nl 2881 [2001:888:2000:d::a6]:52840
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 7577
X-Received-Body-CRC 367906590
Xref csiph.com comp.lang.python:79685

Show key headers only | View raw


On 25.10.2014 19:27, Rustom Mody wrote:
> Moved from other (Seymore's) thread where this is perhaps not relevant
>
> On Saturday, October 25, 2014 1:15:09 PM UTC+5:30, Steven D'Aprano wrote:
>> Rustom Mody wrote:
>>
>>> On Saturday, October 25, 2014 11:20:03 AM UTC+5:30, Chris Angelico wrote:
>>>> On Sat, Oct 25, 2014 at 4:40 PM, Rustom Mody  wrote:
>>>>> Its generally accepted that side-effecting functions are not a good
>>>>> idea -- typically a function that returns something and changes global
>>>>> state.
>>>>
>>>> Only in certain circles. Not in Python. There are large numbers of
>>>> functions with side effects (mutator methods like list.append,
>>>> anything that needs lots of state like random.random, everything with
>>>> external effect like I/O, heaps of stuff), and it is most definitely
>>>> not frowned upon.
>>>>
>>>> In Python 3 (or Python 2 with the future directive), print is a
>>>> function, print() an expression. It's not "semantically a statement".
>>>
>>> Ok
>>> So give me a valid (ie useful) use where instead of the usual
>>> l=[1,2,3]
>>> l.append(4)
>>>
>>> we have
>>>
>>> foo(l.append(4))
>>
>> Your question seems to be non-sequitor. To me, it doesn't appear to have any
>> relationship to Chris' comments.
>
> | Languages like Pascal (many others)... distinguish function which return
> | results and procedure which do not...
> | Extract from https://en.wikipedia.org/wiki/Subroutine#Language_support
>
> So my point: Whether the language supports it strongly (Pascal'
> procedures) weakly (C's void functions) more weakly (Python's None
> returning functions), the beginning programmer needs this concept as a core
> thinking tool.
>
> Pascal makes this easy -- teach the syntax and the concept will get across
> Python is harder because the concept does not correlate with any specific syntax
> But its not all that hard unless the teacher is stuck in correlating core concepts
> and language syntax.
>
> A teacher who is so stuck is cheating the student.
>
> My version:
> "print may (3) or may not (2) be an expression. Just always consider it as a statement"
>
> Chris version: print() is an expression.
> Technically Chris is correct. Is it methodologically/pedagogically it is sound?
>
> Consider:
>
>  From my explanation this:
>
>>>> [print(x) for x in [1,2,3]]
> 1
> 2
> 3
> [None, None, None]
>>>>
>
> is in "Dont Do!" category whether python (3) allows it or python 2 doesn't.
> And you "Dont do" because print(x) is a statement -- literally in
> python 2; morally in python 3
>
> And from here its only a small step to why l.append(4) should never
> be used except as a statement. Python may not literally have Pascal's procedures;
> they are morally there if you choose to 'believe' in them.
>
> How would Chris inculcate avoidance of code-smells like
> - foo(l.append(4))
> - [print(x) for x in [1,2,3]]
>

As Chris and Steven have pointed out, picking print() as an example does 
not make too much sense since it returns None.
It may be rare to use an expression both for its side-effects and its 
return value, but, provided you document the intention appropriately, I 
do not see what would generally be wrong with it.

A quick example that's not quite as silly as all your print() ones:

 >>> with open('longnumber.txt', 'w') as out:
	print(sum(out.write(str(x)) for x in range(100)), 'characters written.')

	
190 characters written.

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


Thread

Status of side-effecting functions in python Rustom Mody <rustompmody@gmail.com> - 2014-10-25 10:27 -0700
  Re: Status of side-effecting functions in python Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-10-25 23:41 +0200
    Re: Status of side-effecting functions in python Dan Sommers <dan@tombstonezero.net> - 2014-10-25 22:22 +0000
      Re: Status of side-effecting functions in python Terry Reedy <tjreedy@udel.edu> - 2014-10-25 18:53 -0400
    Re: Status of side-effecting functions in python wxjmfauth@gmail.com - 2014-10-26 00:45 -0700
      Re: Status of side-effecting functions in python Dan Sommers <dan@tombstonezero.net> - 2014-10-26 13:41 +0000
        Re: Status of side-effecting functions in python Rustom Mody <rustompmody@gmail.com> - 2014-10-26 07:53 -0700
        Re: Status of side-effecting functions in python wxjmfauth@gmail.com - 2014-10-26 09:26 -0700
          Re: Status of side-effecting functions in python Marko Rauhamaa <marko@pacujo.net> - 2014-10-26 18:50 +0200
          Re: Status of side-effecting functions in python Roy Smith <roy@panix.com> - 2014-10-26 17:56 -0400
            Re: Status of side-effecting functions in python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-27 22:30 +1100
              Re: Status of side-effecting functions in python Chris Angelico <rosuav@gmail.com> - 2014-10-27 22:44 +1100
              Re: Status of side-effecting functions in python Roy Smith <roy@panix.com> - 2014-10-27 09:28 -0400
              Re: Status of side-effecting functions in python Grant Edwards <invalid@invalid.invalid> - 2014-10-27 14:45 +0000
                Re: Status of side-effecting functions in python Marko Rauhamaa <marko@pacujo.net> - 2014-10-27 17:14 +0200
                Re: Status of side-effecting functions in python Nobody <nobody@nowhere.invalid> - 2014-10-28 03:52 +0000
                Re: Status of side-effecting functions in python Marko Rauhamaa <marko@pacujo.net> - 2014-10-28 11:07 +0200
                Re: Status of side-effecting functions in python Marko Rauhamaa <marko@pacujo.net> - 2014-10-28 11:10 +0200
                .write() behavior Alan Bawden <alan@scooby-doo.csail.mit.edu> - 2014-10-29 01:06 -0400
                Re: .write() behavior Marko Rauhamaa <marko@pacujo.net> - 2014-10-29 08:00 +0200
                Re: .write() behavior Alan Bawden <alan@scooby-doo.csail.mit.edu> - 2014-10-31 16:32 -0400
    Re: Status of side-effecting functions in python Grant Edwards <invalid@invalid.invalid> - 2014-10-27 14:22 +0000

csiph-web