Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > scope of function parameters

Reply
Thread Tools

scope of function parameters

 
 
Henry Olders
Guest
Posts: n/a
 
      05-29-2011
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




 
Reply With Quote
 
 
 
 
Mel
Guest
Posts: n/a
 
      05-29-2011
Henry Olders wrote:

> 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.


It doesn't look like a question of local or global. fnc2 is passed a
container object and replaces item 1 in that container. You see the results
when fnc2 prints the object it knows as `c`, and you see again when main
prints the object it knows as `a`. Python doesn't pass parameters by
handing around copies that can be thought of as local or global. Python
passes parameters by binding objects to names in the callee's namespace. In
your program the list known as `a` in main is identically the same list as
the one known as `c` in fnc2, and what happens happens.

Mel.

 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      05-29-2011
On 5/29/2011 7:59 AM, Mel wrote:
> Henry Olders wrote:
>
>> 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.


Function *parameters* are names, the first *local names* of the function.

>> It doesn't make sense to me to pass a global to a function as a parameter.


You are right, in a way. Global *names* are just names. When you call
a function, you pass *objects* as *arguments*. Of course, you may refer
to the object by a global name to pass it, or you can pass a string
object that contains a global name.
>
> It doesn't look like a question of local or global. fnc2 is passed a
> container object and replaces item 1 in that container. You see the results
> when fnc2 prints the object it knows as `c`, and you see again when main
> prints the object it knows as `a`. Python doesn't pass parameters by
> handing around copies that can be thought of as local or global. Python
> passes parameters by binding objects to names in the callee's namespace. In
> your program the list known as `a` in main is identically the same list as
> the one known as `c` in fnc2, and what happens happens.


Right. Python has one unnamed 'objectspace'. It has many, many
namespaces: builtins, globals for each module, locals for each function
and class, and attributes for some instances. Each name and each
collection slot is associated with one object. Each object can have
multiple associations, as in the example above.

--
Terry Jan Reedy

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      05-29-2011
On Sun, 29 May 2011 04:30:52 -0400, Henry Olders wrote:

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

[...]
> 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?


The nature of Henry's misunderstanding is a disguised version of the very
common "is Python call by reference or call by value?" question that
periodically crops up. I wrote a long, but I hope useful, explanation for
the http://www.velocityreviews.com/forums/(E-Mail Removed) mailing list, which I'd like to share here:

http://mail.python.org/pipermail/tut...er/080505.html


Constructive criticism welcome.


--
Steven
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      05-30-2011
On Mon, May 30, 2011 at 11:31 AM, Ben Finney <(E-Mail Removed)> wrote:
> *<URL:http://www.computerworld.com/s/article/print/93903/I_m_OK_The_Bull_Is_Dead>


I agree with the gist of that. My take on this is: When I'm talking to
my boss, I always assume that the phone will ring ten seconds into my
explanation. Ten seconds is enough for "Dad, I'm OK; the bull is
dead", it's enough for "I've solved Problem X, we can move on now";
it's enough for "Foo is crashing, can't ship till I debug it". If
fortune is smiling on me and the phone isn't ringing, I can explain
that Problem X was the reason Joe was unable to demo his new module,
or that the crash in Foo is something that I know I'll be able to pin
down in a one-day debugging session, but even if I don't, my boss
knows enough to keep going with.

Of course, there's a significant difference between a mailing list
post and a detailed and well copyedited article. Quite frequently I'll
ramble on list, in a way quite inappropriate to a publication that
would be linked to as a "hey guys, here's how it is" page. Different
media, different standards.

Chris Angelico
"Forty thousand million billion THEGS quotes? That must be worth a fortune!"
-- definitely a fan of THEGS --
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      05-30-2011
On Mon, May 30, 2011 at 12:08 PM, Ben Finney <(E-Mail Removed)> wrote:
> Chris Angelico <(E-Mail Removed)> writes:
>
>> Of course, there's a significant difference between a mailing list
>> post and a detailed and well copyedited article. Quite frequently I'll
>> ramble on list, in a way quite inappropriate to a publication that
>> would be linked to as a "hey guys, here's how it is" page. Different
>> media, different standards.

>
> Right. But Steven specifically asked for constructive criticism, which I
> took as permission to treat the referenced post as an article in need of
> copy editing


Indeed. Was just saying that there are times when you need to get the
slug out first, and times when it's okay to be a little less impactual
(if that's a word). Although it's still important to deliver your
message promptly.

Of course, there are other contexts where you specifically do NOT want
to give everything away at the beginning. Certain styles of rhetoric
demand that you set the scene, build your situation, and only at the
climax reveal what it is you are trying to say.

Ahh! wordsmithing, how we love thee.

Chris Angelico
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      05-30-2011
On Mon, 30 May 2011 11:31:33 +1000, Ben Finney wrote:

> Steven D'Aprano <(E-Mail Removed)> writes:
>
>> http://mail.python.org/pipermail/tut...er/080505.html
>>
>>
>> Constructive criticism welcome.

>
> Informative, but it “buries the lead” as our friends in the press corps
> would say.


Thank you, that's a good point.


[...]
> More on this style:
>
> <URL:http://www.computerworld.com/s/article/print/93903/

I_m_OK_The_Bull_Is_Dead>


Or as they say in the fiction-writing trade, "shoot the sheriff on the
first page".



--
Steven
 
Reply With Quote
 
Laurent Claessens
Guest
Posts: n/a
 
      05-30-2011
Le 29/05/2011 23:42, Ben Finney a écrit :
> Peter Pearson<(E-Mail Removed)> writes:
>
>> Python works in terms of objects having names, and one
>> object can have many names.

>
> Or no names. So it's less accurate (though better than talking of
> “variables”) to speak of Python objects “having names”.


Could you give an example of an object that has no name ? I've missed
something ...

Laurent
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      05-30-2011
On Mon, May 30, 2011 at 12:12 AM, Laurent Claessens <(E-Mail Removed)> wrote:
> Le 29/05/2011 23:42, Ben Finney a écrit :
>> Peter Pearson<(E-Mail Removed)> *writes:
>>
>>> *Python works in terms of objects having names, and one
>>> *object can have many names.

>>
>> Or no names. So it's less accurate (though better than talking of
>> “variables”) to speak of Python objects “having names”.

>
> Could you give an example of an object that has no name ? I've missed
> something ...


def foo():
return 5

print(foo())

The int object 5 has no name here.

Cheers,
Chris
 
Reply With Quote
 
Laurent
Guest
Posts: n/a
 
      05-30-2011

>> Could you give an example of an object that has no name ? I've missed
>> something ...

>
> def foo():
> return 5
>
> print(foo())
>
> The int object 5 has no name here.


Cool. I was thinking that "5" was the name, but
>>> 5.__add__(6)

File "<stdin>", line 1
5.__add__(6)
^
SyntaxError: invalid syntax

while

>>> a=5
>>> a.__add__(6)

11


Very well. I learned something today.
Thanks
Laurent
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
scope of function parameters Esash C Programming 28 02-08-2013 01:25 AM
Re: scope of function parameters (take two) Henry Olders Python 2 05-31-2011 05:03 AM
Re: scope of function parameters Wolfgang Rohdewald Python 8 05-29-2011 07:20 PM
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM



Advertisments