Re: Something in the function tutorial confused me.

2007-08-13 Thread Neil Cerutti
On 2007-08-12, Alex Martelli [EMAIL PROTECTED] wrote: Neil Cerutti [EMAIL PROTECTED] wrote: ... OK, I've thought about this some more and I think the source of my confusion was I thought assignment in Python meant binding a name to something, not mutating an object. But in the case of

Re: Something in the function tutorial confused me.

2007-08-13 Thread Alex Martelli
Neil Cerutti [EMAIL PROTECTED] wrote: ... Then we get into unpacking assignments and augmented assignments, but I don't really want to write two more pages worth of summary...;-). Thanks very much for taking the time to help clear up my erroneous model of assignment in Python. I'd

Re: Finding gurus (was Re: Something in the function tutorial confused me.)

2007-08-13 Thread Ricardo Aráoz
Aahz wrote: In article [EMAIL PROTECTED], Alex Martelli [EMAIL PROTECTED] wrote: Because of this, a Google search for name surname python may sometimes help; when you get 116,000 hits, as for Steve Holden python, that may be a reasonable indication that the poster is one of the world's

Re: Finding gurus (was Re: Something in the function tutorial confused me.)

2007-08-13 Thread kyosohma
On Aug 13, 2:22 pm, Ricardo Aráoz [EMAIL PROTECTED] wrote: Aahz wrote: In article [EMAIL PROTECTED], Alex Martelli [EMAIL PROTECTED] wrote: Because of this, a Google search for name surname python may sometimes help; when you get 116,000 hits, as for Steve Holden python, that may

Re: Finding gurus (was Re: Something in the function tutorial confused me.)

2007-08-13 Thread Steve Holden
[EMAIL PROTECTED] wrote: On Aug 13, 2:22 pm, Ricardo Aráoz [EMAIL PROTECTED] wrote: Aahz wrote: In article [EMAIL PROTECTED], Alex Martelli [EMAIL PROTECTED] wrote: Because of this, a Google search for name surname python may sometimes help; when you get 116,000 hits, as for Steve Holden

Re: Something in the function tutorial confused me.

2007-08-11 Thread Neil Cerutti
On 2007-08-11, Alex Martelli [EMAIL PROTECTED] wrote: Neil Cerutti [EMAIL PROTECTED] wrote: ... The Python Language Reference seems a little confused about the terminology. 3.4.7 Emulating numeric types 6.3.1 Augmented assignment statements The former refers to augmented

Finding gurus (was Re: Something in the function tutorial confused me.)

2007-08-11 Thread Aahz
In article [EMAIL PROTECTED], Alex Martelli [EMAIL PROTECTED] wrote: Because of this, a Google search for name surname python may sometimes help; when you get 116,000 hits, as for Steve Holden python, that may be a reasonable indication that the poster is one of the world's Python Gurus (in

Augmented assignment (was Re: Something in the function tutorial confused me.)

2007-08-11 Thread Aahz
In article [EMAIL PROTECTED], Neil Cerutti [EMAIL PROTECTED] wrote: On 2007-08-11, Alex Martelli [EMAIL PROTECTED] wrote: Neil Cerutti [EMAIL PROTECTED] wrote: ... The Python Language Reference seems a little confused about the terminology. 3.4.7 Emulating numeric types 6.3.1

Re: Finding gurus (was Re: Something in the function tutorial confused me.)

2007-08-11 Thread Steve Holden
Aahz wrote: In article [EMAIL PROTECTED], Alex Martelli [EMAIL PROTECTED] wrote: Because of this, a Google search for name surname python may sometimes help; when you get 116,000 hits, as for Steve Holden python, that may be a reasonable indication that the poster is one of the world's

Re: Augmented assignment (was Re: Something in the function tutorial confused me.)

2007-08-11 Thread Roel Schroeven
Aahz schreef: In article [EMAIL PROTECTED], Neil Cerutti [EMAIL PROTECTED] wrote: On 2007-08-11, Alex Martelli [EMAIL PROTECTED] wrote: Neil Cerutti [EMAIL PROTECTED] wrote: ... The Python Language Reference seems a little confused about the terminology. 3.4.7 Emulating numeric

Re: Augmented assignment (was Re: Something in the function tutorial confused me.)

2007-08-11 Thread Aahz
In article [EMAIL PROTECTED], Roel Schroeven [EMAIL PROTECTED] wrote: Aahz schreef: Although Alex is essentially correct, the situation is a bit more complex and you are correct that augmented assignment allows the object to decide whether to mutate in place. However, the critical part of

Re: Augmented assignment (was Re: Something in the function tutorial confused me.)

2007-08-11 Thread Roel Schroeven
Aahz schreef: def foo(bar): bar[0] += ['zap'] ... import dis dis.dis(foo) 1 0 LOAD_FAST0 (bar) 3 LOAD_CONST 1 (0) 6 DUP_TOPX 2 9 BINARY_SUBSCR 10 LOAD_CONST

Re: Augmented assignment (was Re: Something in the function tutorial confused me.)

2007-08-11 Thread OKB (not okblacke)
Aahz wrote: tup=([],) tup[0] += ['zap'] Traceback (most recent call last): File stdin, line 1, in module TypeError: 'tuple' object does not support item assignment snip Obviously, you can easily work around it: t = ([],) l = t[0] l += ['foo'] t (['foo'],) This is quite

Re: Augmented assignment (was Re: Something in the function tutorial confused me.)

2007-08-11 Thread Roel Schroeven
OKB (not okblacke) schreef: Aahz wrote: tup=([],) tup[0] += ['zap'] Traceback (most recent call last): File stdin, line 1, in module TypeError: 'tuple' object does not support item assignment snip Obviously, you can easily work around it: t = ([],) l = t[0] l += ['foo'] t

Re: Something in the function tutorial confused me.

2007-08-11 Thread Gregory D. Weber
Neil Cerutti wrote: On 2007-08-11, Alex Martelli [EMAIL PROTECTED] wrote: Neil Cerutti [EMAIL PROTECTED] wrote: ... The Python Language Reference seems a little confused about the terminology. 3.4.7 Emulating numeric types 6.3.1 Augmented assignment statements The former refers to

Re: Something in the function tutorial confused me.

2007-08-11 Thread Roel Schroeven
Gregory D. Weber schreef: Neil Cerutti wrote: On 2007-08-11, Alex Martelli [EMAIL PROTECTED] wrote: Neil Cerutti [EMAIL PROTECTED] wrote: ... The Python Language Reference seems a little confused about the terminology. 3.4.7 Emulating numeric types 6.3.1 Augmented assignment

Re: Augmented assignment (was Re: Something in the function tutorial confused me.)

2007-08-11 Thread Aahz
In article [EMAIL PROTECTED], OKB (not okblacke) [EMAIL PROTECTED] wrote: This sentence is phrased as though it is the whole story, but it isn't, because the operation might not in fact wind up being an assignment. Shouldn't there be an except see below or something there, to alert the

Re: Augmented assignment (was Re: Something in the function tutorial confused me.)

2007-08-11 Thread Aahz
In article [EMAIL PROTECTED], Roel Schroeven [EMAIL PROTECTED] wrote: I used to interpret the target in 'The target is only evaluated once' more like an L-value in C/C++. That's not correct, of course, but I didn't understand exactly how wrong it was until now. It's true almost everywhere

Re: Something in the function tutorial confused me.

2007-08-11 Thread David Wahler
On 8/11/07, Gregory D. Weber [EMAIL PROTECTED] wrote: I too thought += was an assignment. And it bit me very painfully a few weeks ago. If it's an assignment, then wouldn't x += y mean the same thing as x = x + y? If so, why does an assignment to variable a, below, have the *side effect*

Re: Something in the function tutorial confused me.

2007-08-11 Thread Neil Cerutti
On 2007-08-11, Alex Martelli [EMAIL PROTECTED] wrote: Neil Cerutti [EMAIL PROTECTED] wrote: ... The Python Language Reference seems a little confused about the terminology. 3.4.7 Emulating numeric types 6.3.1 Augmented assignment statements The former refers to augmented

Re: Something in the function tutorial confused me.

2007-08-11 Thread Alex Martelli
Neil Cerutti [EMAIL PROTECTED] wrote: ... OK, I've thought about this some more and I think the source of my confusion was I thought assignment in Python meant binding a name to something, not mutating an object. But in the case of augmented assignment, assignment no longer means that?

Re: Something in the function tutorial confused me.

2007-08-10 Thread Alex Martelli
greg [EMAIL PROTECTED] wrote: Steve Holden wrote: For some reason your reply got right up my nose, I'm sorry about that. Sometimes it's hard to judge the level of experience with Python that a poster has. In Because of this, a Google search for name surname python may sometimes

Re: Something in the function tutorial confused me.

2007-08-10 Thread Alex Martelli
Neil Cerutti [EMAIL PROTECTED] wrote: ... The Python Language Reference seems a little confused about the terminology. 3.4.7 Emulating numeric types 6.3.1 Augmented assignment statements The former refers to augmented arithmetic operations, which I think is a nice terminology,

Re: Something in the function tutorial confused me.

2007-08-09 Thread Magnus Lycka
Lee Fleming wrote: Hello, I have a simple question. Say you have the following function: def f(x, y = []): ... But this, the code that fixes the list accumulation confounds me: def f(x, y=None): if y is None: y = [] ... In other words, what's going on here? How is it that y

Re: Something in the function tutorial confused me.

2007-08-09 Thread Neil Cerutti
On 2007-08-09, Magnus Lycka [EMAIL PROTECTED] wrote: It's all a matter of understanding that all the juicy bits in the Python data model is in the actual values or objects. That's the stuff with type safety, a location in memory, qualities such as mutability etc. A variable is basically just

Re: Something in the function tutorial confused me.

2007-08-08 Thread Steve Holden
greg wrote: Steve Holden wrote: OK. The difference is that [] is a mutable value, while None is immutable. No, it's not. It has nothing to do with mutability vs immutability. I beg to differ. That has everything to do with it. The difference is that in the first version the expression

Re: Something in the function tutorial confused me.

2007-08-08 Thread Carsten Haese
On Wed, 2007-08-08 at 08:52 -0400, Steve Holden wrote: While it's correct that rebinding y will usually cause it to reference a different object, this need not be true of assignment. The augmented assignment operations do no necessarily rebind their left-hand operand - that depends on the

Re: Something in the function tutorial confused me.

2007-08-08 Thread Steve Holden
Carsten Haese wrote: On Wed, 2007-08-08 at 08:52 -0400, Steve Holden wrote: [...] The problem is your ambiguous use of the word assignment. In the sense of the Language Reference, any assignment to y is a simple assignment that always modifies a namespace and never modifies an object. In

Re: Something in the function tutorial confused me.

2007-08-08 Thread Neil Cerutti
On 2007-08-08, Steve Holden [EMAIL PROTECTED] wrote: Carsten Haese wrote: On Wed, 2007-08-08 at 08:52 -0400, Steve Holden wrote: [...] The problem is your ambiguous use of the word assignment. In the sense of the Language Reference, any assignment to y is a simple assignment that always

Re: Something in the function tutorial confused me.

2007-08-08 Thread greg
Steve Holden wrote: For some reason your reply got right up my nose, I'm sorry about that. Sometimes it's hard to judge the level of experience with Python that a poster has. In discussions about this particular topic, often it turns out that the person is a newcomer to Python who is using the

Re: Something in the function tutorial confused me.

2007-08-08 Thread Steve Holden
greg wrote: Steve Holden wrote: For some reason your reply got right up my nose, I'm sorry about that. Sometimes it's hard to judge the level of experience with Python that a poster has. In discussions about this particular topic, often it turns out that the person is a newcomer to

Re: Something in the function tutorial confused me.

2007-08-07 Thread Ricardo Aráoz
Lee Fleming wrote: Thanks for all the help, everyone. I guess I was confused with default arguments that were mutable and immutable. I will continue to look over these posts until I understand what is happening. I cannot believe the number of helpful responses I got! Apparently he didn't

Re: Something in the function tutorial confused me.

2007-08-07 Thread David Wahler
On 8/7/07, Ricardo Aráoz [EMAIL PROTECTED] wrote: Lee Fleming wrote: Thanks for all the help, everyone. I guess I was confused with default arguments that were mutable and immutable. I will continue to look over these posts until I understand what is happening. I cannot believe the

Re: Something in the function tutorial confused me.

2007-08-07 Thread Steve Holden
Ricardo Aráoz wrote: Lee Fleming wrote: Thanks for all the help, everyone. I guess I was confused with default arguments that were mutable and immutable. I will continue to look over these posts until I understand what is happening. I cannot believe the number of helpful responses I got!

Re: Something in the function tutorial confused me.

2007-08-07 Thread greg
Steve Holden wrote: OK. The difference is that [] is a mutable value, while None is immutable. No, it's not. It has nothing to do with mutability vs immutability. The difference is that in the first version the expression [] is evaluated only *once*, when the function is defined. Therefore

Something in the function tutorial confused me.

2007-08-06 Thread Lee Fleming
Hello, I have a simple question. Say you have the following function: def f(x, y = []): y.append(x) return y print f(23) # prints [23] print f(42) # prints [23, 42] As far as I understand, the default value y, an empty list, is created when the def statement evaluates. With this

Re: Something in the function tutorial confused me.

2007-08-06 Thread Marc 'BlackJack' Rintsch
On Sun, 05 Aug 2007 23:50:24 -0700, Lee Fleming wrote: But this, the code that fixes the list accumulation confounds me: def f(x, y=None): if y is None: y = [] y.append(x) return y print f(23) # prints [23] print f(42) # prints [42] Why didn't the second call to f,

Re: Something in the function tutorial confused me.

2007-08-06 Thread Stargaming
On Sun, 05 Aug 2007 23:50:24 -0700, Lee Fleming wrote: Hello, I have a simple question. Say you have the following function: def f(x, y = []): y.append(x) return y print f(23) # prints [23] print f(42) # prints [23, 42] As far as I understand, the default value y, an empty

Re: Something in the function tutorial confused me.

2007-08-06 Thread Alex Popescu
Stargaming [EMAIL PROTECTED] wrote in news:46b6df49$0$26165 [EMAIL PROTECTED]: On Sun, 05 Aug 2007 23:50:24 -0700, Lee Fleming wrote: Hello, I have a simple question. Say you have the following function: def f(x, y = []): y.append(x) return y print f(23) # prints [23] print

Re: Something in the function tutorial confused me.

2007-08-06 Thread Neil Cerutti
On 2007-08-06, Lee Fleming [EMAIL PROTECTED] wrote: I have a simple question. Say you have the following function: def f(x, y = []): y.append(x) return y print f(23) # prints [23] print f(42) # prints [23, 42] def f(x, y=None): if y is None: y = [] y.append(x)

Re: Something in the function tutorial confused me.

2007-08-06 Thread Neil Cerutti
On 2007-08-06, Neil Cerutti [EMAIL PROTECTED] wrote: On 2007-08-06, Lee Fleming [EMAIL PROTECTED] wrote: def f(x, y=None): if y is None: y = [] y.append(x) return y f(f(23)) [23, 42] Sorry. That should've been: f(42, f(23)) [23, 42] -- Neil Cerutti Scouts are saving

Re: Something in the function tutorial confused me.

2007-08-06 Thread Stargaming
On Mon, 06 Aug 2007 11:13:45 +, Alex Popescu wrote: Stargaming [EMAIL PROTECTED] wrote in news:46b6df49$0$26165 [EMAIL PROTECTED]: [snip] You're just unluckily shadowing the name `y` in the local scope of your function. Your code snippet could be rewritten as:: def f(x, y=None):

Re: Something in the function tutorial confused me.

2007-08-06 Thread Lee Fleming
On Aug 6, 6:25 am, Neil Cerutti [EMAIL PROTECTED] wrote: Because when the function is called, the line if y is None: y = [] is executed, binding a brand new empty list to y. This rebinding happens every time the function is called, unless you provide an argument for y that is not None.

RE: Something in the function tutorial confused me.

2007-08-06 Thread Hamilton, William
From: Lee Fleming On Aug 6, 6:25 am, Neil Cerutti [EMAIL PROTECTED] wrote: Because when the function is called, the line if y is None: y = [] is executed, binding a brand new empty list to y. This rebinding happens every time the function is called, unless you provide an

Re: Something in the function tutorial confused me.

2007-08-06 Thread Lee Fleming
On Aug 6, 12:30 pm, Hamilton, William [EMAIL PROTECTED] wrote: When you call f(23), the variable y within it gets created and points at None. When f(23) exits, the y that it created gets destroyed. (Well, goes out of scope, but even if it's not garbage collected it won't ever come back into

Re: Something in the function tutorial confused me.

2007-08-06 Thread Neil Cerutti
On 2007-08-06, Lee Fleming [EMAIL PROTECTED] wrote: On Aug 6, 6:25 am, Neil Cerutti [EMAIL PROTECTED] wrote: Because when the function is called, the line if y is None: y = [] is executed, binding a brand new empty list to y. This rebinding happens every time the function is called,

Re: Something in the function tutorial confused me.

2007-08-06 Thread Neil Cerutti
On 2007-08-06, Lee Fleming [EMAIL PROTECTED] wrote: On Aug 6, 12:30 pm, Hamilton, William [EMAIL PROTECTED] wrote: When you call f(23), the variable y within it gets created and points at None. When f(23) exits, the y that it created gets destroyed. (Well, goes out of scope, but even if

Re: Something in the function tutorial confused me.

2007-08-06 Thread Marc 'BlackJack' Rintsch
On Mon, 06 Aug 2007 10:51:20 -0700, Lee Fleming wrote: why isn't the y in def f (x, y = []): something garbage-collected? `y` is a name. Only objects are garbage collected. There is no `y` in that ``def`` in the sense that a local name `y` exists when the ``def`` is executed. The line just

RE: Something in the function tutorial confused me.

2007-08-06 Thread Hamilton, William
From: Lee Fleming On Aug 6, 12:30 pm, Hamilton, William [EMAIL PROTECTED] wrote: When you call f(23), the variable y within it gets created and points at None. When f(23) exits, the y that it created gets destroyed. (Well, goes out of scope, but even if it's not garbage collected it

Re: Something in the function tutorial confused me.

2007-08-06 Thread Lee Fleming
Thanks for all the help, everyone. I guess I was confused with default arguments that were mutable and immutable. I will continue to look over these posts until I understand what is happening. I cannot believe the number of helpful responses I got! --

Re: Something in the function tutorial confused me.

2007-08-06 Thread Steve Holden
Lee Fleming wrote: On Aug 6, 6:25 am, Neil Cerutti [EMAIL PROTECTED] wrote: Because when the function is called, the line if y is None: y = [] is executed, binding a brand new empty list to y. This rebinding happens every time the function is called, unless you provide an

Re: Something in the function tutorial confused me.

2007-08-06 Thread Lee Fleming
On Aug 6, 1:26 pm, Steve Holden [EMAIL PROTECTED] wrote: Lee Fleming wrote: On Aug 6, 6:25 am, Neil Cerutti [EMAIL PROTECTED] wrote: Because when the function is called, the line if y is None: y = [] is executed, binding a brand new empty list to y. This rebinding happens every

Re: Something in the function tutorial confused me.

2007-08-06 Thread Steve Holden
Marc 'BlackJack' Rintsch wrote: On Mon, 06 Aug 2007 10:51:20 -0700, Lee Fleming wrote: why isn't the y in def f (x, y = []): something garbage-collected? `y` is a name. Only objects are garbage collected. There is no `y` in that ``def`` in the sense that a local name `y` exists when the