473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Inconsistent reaction to extend

Gurus, before I am tempted to signal this as a bug, perhaps
you might convince me that it should be so. If I type

l=range(4)
l.extend([1,2])

l gives [0,1,2,3,1,2], what else...

On the other hand, try

p=range(4).extend([1,2])

Then, p HAS NO VALUE (NoneType).

With append the behaviour is similar. I didn't try other methods, but
I suspect that it won't improve.
WHY?
It seems that there was already some discussion about consistency and
somebody produced the example: h = {}.update(l) which didn't work,
but I wasn't subscribed to this nsgr, I couldn't follow this affair.

Jerzy Karczmarczuk
Sep 9 '05 #1
10 2005

Jerzy Karczmarczuk kirjoitti:
On the other hand, try

p=range(4).extend([1,2])

Then, p HAS NO VALUE (NoneType).

With append the behaviour is similar. I didn't try other methods, but
I suspect that it won't improve.
WHY?


range(4) returns a list and Python's list.extend() returns None. Extend
is a in-place operation.

-- timo

Sep 9 '05 #2
Jerzy Karczmarczuk wrote:
Gurus, before I am tempted to signal this as a bug, perhaps
you might convince me that it should be so.
it's not a bug, and nobody should have to convince you about any-
thing; despite what you may think from reading certain slicing threads,
this mailing list is not an argument clinic.
If I type

l=range(4)
l.extend([1,2])

l gives [0,1,2,3,1,2], what else...

On the other hand, try

p=range(4).extend([1,2])

Then, p HAS NO VALUE (NoneType).
(footnote: None is a value in Python. it can be used to represent "no value",
but it can also be used for other things)
With append the behaviour is similar. I didn't try other methods, but
I suspect that it won't improve.

WHY?


because you're saving the return value of "extend", not "range", and "extend"
returns None.

if you split it up like this

l = range(4)
p = l.extend([1, 2])

it should be obvious that "l" and "p" doesn't necessarily contain the same thing.

</F>

Sep 9 '05 #3
Jerzy Karczmarczuk wrote:
Gurus, before I am tempted to signal this as a bug, perhaps
you might convince me that it should be so. If I type

l=range(4)
l.extend([1,2])

l gives [0,1,2,3,1,2], what else...

On the other hand, try

p=range(4).extend([1,2])

Then, p HAS NO VALUE (NoneType).

With append the behaviour is similar. I didn't try other methods, but
I suspect that it won't improve.

WHY?


..append(), .extend(), .sort() (as well as .update() for dictionaries)
all are methods whose *only* effect is to modify the object in-place.
They return None as a reminder that they do modify the object instead of
copying the object and then modifying the copy. From the FAQ[1] with
respect to .sort():

"""This way, you won't be fooled into accidentally overwriting a list
when you need a sorted copy but also need to keep the unsorted version
around."""

[1]
http://www.python.org/doc/faq/genera...he-sorted-list

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Sep 9 '05 #4
On Fri, 09 Sep 2005 11:47:41 +0200, Jerzy Karczmarczuk wrote:
Gurus, before I am tempted to signal this as a bug, perhaps
you might convince me that it should be so. If I type

l=range(4)
l.extend([1,2])

l gives [0,1,2,3,1,2], what else...
That is correct. range() returns a list. You then call the extend method
on that list. Extend has deliberate side-effects: it operates on the list
that calls it, it does not create a new list.
On the other hand, try

p=range(4).extend([1,2])

Then, p HAS NO VALUE (NoneType).
p has the value None, which is also correct. The extend() method returns
None, it does not create a new list. There is nothing inconsistent about
it. Unintuitive, perhaps. Unexpected, maybe. But not inconsistent.

[snip]
WHY?


Because creating a new list is potentially very time-consuming and
expensive of memory. Imagine you have a list of 100,000 large objects, and
you want to add one more object to it. The way Python works is that append
and extend simply add that new object to the end of the existing list. The
way you imagined it would work would require Python to duplicate the
entire list, all 100,000 large objects, plus the extra one.

--
Steven.

Sep 9 '05 #5
Op 2005-09-09, Steven D'Aprano schreef <st***@REMOVETHIScyber.com.au>:
On Fri, 09 Sep 2005 11:47:41 +0200, Jerzy Karczmarczuk wrote:
Gurus, before I am tempted to signal this as a bug, perhaps
you might convince me that it should be so. If I type

l=range(4)
l.extend([1,2])

l gives [0,1,2,3,1,2], what else...


That is correct. range() returns a list. You then call the extend method
on that list. Extend has deliberate side-effects: it operates on the list
that calls it, it does not create a new list.
On the other hand, try

p=range(4).extend([1,2])

Then, p HAS NO VALUE (NoneType).


p has the value None, which is also correct. The extend() method returns
None, it does not create a new list. There is nothing inconsistent about
it. Unintuitive, perhaps. Unexpected, maybe. But not inconsistent.

[snip]
WHY?


Because creating a new list is potentially very time-consuming and
expensive of memory. Imagine you have a list of 100,000 large objects, and
you want to add one more object to it. The way Python works is that append
and extend simply add that new object to the end of the existing list. The
way you imagined it would work would require Python to duplicate the
entire list, all 100,000 large objects, plus the extra one.


This has nothing to do with the need to create a new list.

The extend method could just have returned self. Just as sort and reverse could
have done so. This would have made it possible to do things like the following:

lst.sort().reverse()

instead of having to write:

lst.sort()
lst.reverse()

--
Antoon Pardon
Sep 9 '05 #6
Antoon Pardon a écrit :
Because creating a new list is potentially very time-consuming and
expensive of memory. Imagine you have a list of 100,000 large objects, and
you want to add one more object to it. The way Python works is that append
and extend simply add that new object to the end of the existing list. The
way you imagined it would work would require Python to duplicate the
entire list, all 100,000 large objects, plus the extra one.

This has nothing to do with the need to create a new list.

The extend method could just have returned self. Just as sort and reverse could
have done so. This would have made it possible to do things like the following:

lst.sort().reverse()

instead of having to write:

lst.sort()
lst.reverse()


This was done on purpose to avoid a second class of problems. The one
where you forget that reverse modifies the list in place. That second
class of problems was deemed more dangerous because it works without
immediate errors.
Sep 9 '05 #7
Op 2005-09-09, Christophe schreef <ch*************@free.fr>:
Antoon Pardon a écrit :
Because creating a new list is potentially very time-consuming and
expensive of memory. Imagine you have a list of 100,000 large objects, and
you want to add one more object to it. The way Python works is that append
and extend simply add that new object to the end of the existing list. The
way you imagined it would work would require Python to duplicate the
entire list, all 100,000 large objects, plus the extra one.

This has nothing to do with the need to create a new list.

The extend method could just have returned self. Just as sort and reverse could
have done so. This would have made it possible to do things like the following:

lst.sort().reverse()

instead of having to write:

lst.sort()
lst.reverse()


This was done on purpose to avoid a second class of problems.


I know, but I didn't think it was relevant enough for what I was addressing.
The one
where you forget that reverse modifies the list in place. That second
class of problems was deemed more dangerous because it works without
immediate errors.


I know this is the explanation that is frequently given, but I find
that rather weak. IMO one could argue that the following code
has the same kind of problem as you mention.

lst2 = lst1
lst1.reverse()

If you forget that lst2 is not a copy of lst1 but just an other
name for the same object you are in trouble too and this too
works without immediate errors. So why should lst.sort().reverse()
be so much more dangerous?

Well it doesn't matter much. Python will probably not change in
this respect in the future, so unless you can give me an
argument I haven't heard before I'll just drop this.

--
Antoon Pardon
Sep 9 '05 #8
Fredrik Lundh wrote:
Jerzy Karczmarczuk wrote:

Gurus, before I am tempted to signal this as a bug, perhaps
you might convince me that it should be so.

it's not a bug, and nobody should have to convince you about any-
thing; despite what you may think from reading certain slicing threads,
this mailing list is not an argument clinic.

Yes it is :)

then-there's-the-£10-argument-next-door-ly y'rs - steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Sep 9 '05 #9
On Friday 09 September 2005 08:29 am, Steve Holden wrote:
Yes it is :)


That's not an argument! That's just a contradiction.

I'm not payin' for this!
--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Sep 9 '05 #10
Jerzy Karczmarczuk a écrit :
Gurus,
No guru answered, so you'll have to bear with me...
before I am tempted to signal this as a bug, perhaps
you might convince me that it should be so. If I type

l=range(4)
l.extend([1,2])

l gives [0,1,2,3,1,2], what else...

On the other hand, try

p=range(4).extend([1,2]) Then, p HAS NO VALUE (NoneType).
But it does have a value : None. And no need to shout, we here you
pretty well.

Two questions :
1/ Do you *really* think that, *if* it was a bug, no-one would have
noticed ? Seriously ?

2/ Did you Read The Fine Manual(tm) ?

WHY?


BECAUSE!

'destructive' methods like append, extend, sort etc return None, so you
cannot use'em without knowing they are destructive. Rationale is that it
helps the non-programmers avoiding shooting themselves in the foot. I
personnaly find it's a PITA, but what, you'll have to live with it, or
use another language - I, personnaly, choosed to live with it.

BTW, what's wrong with:
p = range(4) + [1, 2]

Sep 12 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Marina | last post by:
Here is the problem. If 2 different properties on the same (or different) control are bound to the same data column, changing the Text property and calling EndCurrentEdit discards the new value. ...
2
by: Boobie | last post by:
I switched to using this function to create element: ---------------------------------------------------- function elem(name, attrs, style, text) { var e = document.createElement(name); if...
3
by: codeman | last post by:
Hi all Lets say we have two tables: Customer: Customer_number : Decimal(15) Name : Char(30) Purchase: Purchase_number : Decimal(15)
2
by: MrNobody | last post by:
I am trying to extend TreeView to add some of my own functionality and I needed to put a public method but the compiler won't let me and I really don't understand why. I declared my class: ...
20
by: Francine.Neary | last post by:
I am learning C, having fun with strings & pointers at the moment! The following program is my solution to an exercise to take an input, strip the first word, and output the rest. It works fine...
3
by: jacobstr | last post by:
I've noticed Object.extend used in a few different ways and I'm having trouble distinguishing why certain usages apply to a given situation. On line 804 Ajax.Base is defined as follows: ...
3
by: =?iso-8859-1?q?Christian_R=FChl?= | last post by:
Hi folks! I have a little noob problem here with XSLT. I have a XML file that looks like this: <archiveFiles> <module path="c:/temp/module_m17_blabla.tmp"/> <module...
3
by: Rahul Babbar | last post by:
Hi All, When could be the possible reasons that could make a database inconsistent? I was told by somebody that it could become inconsistent if you " force all the applications to a close on...
0
by: johnthawk | last post by:
In the below code setting cell to inconsistent sets entire column inconsistent (renderer).However, I need a third state off | on | inconsistent . How can I change one path->cell? Any help...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.