Why doesn't this work?

Jeremy Jones cypher_dpg at yahoo.com
Fri Dec 7 17:45:46 EST 2001


* Courageous (jkraska at san.rr.com) wrote:
> 
> Python 2.2b2 (#26, Nov 16 2001, 11:44:11) [MSC 32 bit (Intel)] on
> win32
> Type "copyright", "credits" or "license" for more information.
> IDLE 0.8 -- press F1 for help
> >>> def f(): return [1,2,3]
> 
> >>> f()
> [1, 2, 3]
> >>> f().append(4)
> >>> l=f().append(4)
> >>> l
> >>> 

I'll let someone else address why this doesn't work.  I couldn't tell you.  Intuitively, it really looks like it should not work and that Python is behaving properly.  But if you do this:

>>> def f():
	return [1,2,3]

>>> f()
[1, 2, 3]
>>> l = f()
>>> l
[1, 2, 3]
>>> l.append(4)
>>> l
[1, 2, 3, 4]
>>> 

I think you get behavior closer to what you were looking for.  When you did:
> >>> f().append(4)
it doesn't seem like the [1, 2, 3] "IS" anywhere for you to append anything to it.  It seems like you should have to put it into something before you can do anything with it.


Jeremy Jones




More information about the Python-list mailing list