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


Groups > comp.lang.python > #6521

scope of function parameters

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <henry.olders@mcgill.ca>
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; 'python,': 0.01; 'parameter': 0.05; 'snippet': 0.07; 'declarations': 0.09; 'parameter.': 0.09; 'subject:parameters': 0.09; 'output': 0.11; 'def': 0.12; 'debugging': 0.14; "['a": 0.16; 'adopted': 0.16; 'function?': 0.16; 'illustrates': 0.16; 'made-up': 0.16; 'main():': 0.16; 'subject:function': 0.16; 'ugly.': 0.16; 'workaround': 0.16; 'variable': 0.21; 'seems': 0.21; 'here?': 0.23; 'code': 0.24; "doesn't": 0.25; 'function': 0.25; 'changed': 0.25; 'statement': 0.26; 'pass': 0.27; 'changing': 0.28; 'problem': 0.28; 'print': 0.31; 'to:addr:python-list': 0.33; 'rule': 0.34; 'there': 0.35; 'function.': 0.35; 'received:24': 0.35; 'received:ca': 0.36; 'considered': 0.36; 'charset:us-ascii': 0.36; 'something': 0.37; 'but': 0.38; 'third': 0.38; 'should': 0.39; 'unless': 0.39; 'spent': 0.39; 'list,': 0.39; 'to:addr:python.org': 0.39; 'missing': 0.40; 'below': 0.61; 'order': 0.62; 'received:videotron.ca': 0.84
MIME-version 1.0
Content-transfer-encoding 7BIT
Content-type text/plain; charset=us-ascii
From Henry Olders <henry.olders@mcgill.ca>
Subject scope of function parameters
Date Sun, 29 May 2011 04:30:52 -0400
To python-list@python.org
X-Mailer Apple Mail (2.1084)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.2216.1306661462.9059.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 82.94.164.166
X-Trace 1306661462 news.xs4all.nl 49046 [::ffff:82.94.164.166]:33531
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:6521

Show key headers only | View raw


I just spent a considerable amount of time and effort debugging a program. The made-up code snippet below illustrates the problem I encountered:

def main():
	a = ['a list','with','three elements']
	print a
	print fnc1(a)
	print a
	
def fnc1(b):
	return fnc2(b)

def fnc2(c):
	c[1] = 'having'
	return c

This is the output:
['a list', 'with', 'three elements']
['a list', 'having', 'three elements']
['a list', 'having', 'three elements']

I had expected the third print statement to give the same output as the first, but variable a had been changed by changing variable c in fnc2.

It seems that in Python, a variable inside a function is global unless it's assigned. This rule has apparently been adopted in order to reduce clutter by not having to have global declarations all over the place.

I would have thought that a function parameter would automatically be considered local to the function. It doesn't make sense to me to pass a global to a function as a parameter.

One workaround is to call a function with a copy of the list, eg in fnc1 I would have the statement "return fnc2(b[:]". But this seems ugly.

Are there others who feel as I do that a function parameter should always be local to the function? Or am I missing something here?

Henry



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


Thread

scope of function parameters Henry Olders <henry.olders@mcgill.ca> - 2011-05-29 04:30 -0400
  Re: scope of function parameters Mel <mwilson@the-wire.com> - 2011-05-29 07:59 -0400
    Re: scope of function parameters Terry Reedy <tjreedy@udel.edu> - 2011-05-29 16:27 -0400
  Re: scope of function parameters Peter Pearson <ppearson@nowhere.invalid> - 2011-05-29 17:21 +0000
    Re: scope of function parameters Ben Finney <ben+python@benfinney.id.au> - 2011-05-30 07:42 +1000
      Re: scope of function parameters Laurent Claessens <moky.math@gmail.com> - 2011-05-30 09:12 +0200
        Re: scope of function parameters Chris Rebert <clp2@rebertia.com> - 2011-05-30 00:27 -0700
        Re: scope of function parameters Laurent <moky.math@gmail.com> - 2011-05-30 09:38 +0200
        Re: scope of function parameters Daniel Kluev <dan.kluev@gmail.com> - 2011-05-30 19:10 +1100
        Re: scope of function parameters Terry Reedy <tjreedy@udel.edu> - 2011-05-30 05:02 -0400
          Re: scope of function parameters Laurent Claessens <moky.math@gmail.com> - 2011-05-30 11:08 +0200
            Re: scope of function parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-30 09:14 +0000
            Re: scope of function parameters Ben Finney <ben+python@benfinney.id.au> - 2011-05-30 19:17 +1000
            Re: scope of function parameters Peter Otten <__peter__@web.de> - 2011-05-30 11:41 +0200
              Re: scope of function parameters Laurent Claessens <moky.math@gmail.com> - 2011-05-30 12:10 +0200
              Re: scope of function parameters Laurent Claessens <moky.math@gmail.com> - 2011-05-30 12:10 +0200
            Re: scope of function parameters Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2011-05-30 13:40 +0300
            Re: scope of function parameters Terry Reedy <tjreedy@udel.edu> - 2011-05-30 12:49 -0400
        Re: scope of function parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-30 09:16 +0000
  Re: scope of function parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-29 23:46 +0000
    Re: scope of function parameters Ben Finney <ben+python@benfinney.id.au> - 2011-05-30 11:31 +1000
      Re: scope of function parameters Chris Angelico <rosuav@gmail.com> - 2011-05-30 11:56 +1000
        Re: scope of function parameters Ben Finney <ben+python@benfinney.id.au> - 2011-05-30 12:08 +1000
          Re: scope of function parameters Chris Angelico <rosuav@gmail.com> - 2011-05-30 12:37 +1000
      Re: scope of function parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-30 04:23 +0000
  Re: scope of function parameters rusi <rustompmody@gmail.com> - 2011-05-31 09:46 -0700
    Re: scope of function parameters rusi <rustompmody@gmail.com> - 2011-05-31 10:33 -0700

csiph-web