L = [4,3,2,1]
L=L.sort()
L will refer to None, why L.sort() don't return the L?
I want to ask why the designer of Python do so? 11 6075 an****@gmail.com wrote: L = [4,3,2,1] L=L.sort() L will refer to None, why L.sort() don't return the L? I want to ask why the designer of Python do so? http://www.python.org/doc/faq/genera...he-sorted-list
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco an****@gmail.com wrote: L = [4,3,2,1] L=L.sort() L will refer to None, why L.sort() don't return the L? I want to ask why the designer of Python do so?
Because that's the convention that signifies that a Python method
mutates the object rather than returns a new one.
--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Sitting in the den and / Looking at the phone as if it owed / Owed me
a favor -- Blu Cantrell
"an****@gmail.com" <an****@gmail.com> writes: I want to ask why the designer of Python do so?
I'm not a Python core developer nor a designer but I've always known that
sort() is a in-place sort and since the list is a mutable object it mutates the
list sending the "sort()" message. If you want to get back a sorted iterable
use... sorted :)
L = [3, 1, 2]
ls = sorted(L)
now ls is your list, sorted.
--
Lawrence - http://www.oluyede.org/blog
"Nothing is more dangerous than an idea
if it's the only one you have" - E. A. Chartier
Thanks a lot!
However, I wonder why L.sort() don't return the reference L, the
performance of return L and None may be the same. If L.sort() return
L, we shouldn't do the awful things such as:
keys = dict.keys()
keys.sort()
for key in keys:
...do whatever with dict[key]...
we can only write the code as follows:
for key in dict.iterkeys().sort():
...do whatever with dict[key]...
Why?
"an****@gmail.com" <an****@gmail.com> writes: However, I wonder why L.sort() don't return the reference L, the performance of return L and None may be the same.
It's not "the same". sort() does not return anything.
Why?
I've just explained to you and so the others: by default operations on mutable
objects are in place.
s = "abc"
s.upper()
does return another string. String are immutable references.
--
Lawrence - http://www.oluyede.org/blog
"Nothing is more dangerous than an idea
if it's the only one you have" - E. A. Chartier an****@gmail.com enlightened us with: However, I wonder why L.sort() don't return the reference L, the performance of return L and None may be the same.
It's probably because it would become confusing. Many people don't
read the documentation. If L.sort() returns a sorted version of L,
they would probably assume it didn't do an in-place sort. The effects
of an unexpected in-place sort are much harder to track down and debug
than a function returning None.
Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
So you write:
for key in sorted(dict.iterkeys()):
... do it ...
dict.iterkeys() returns an iterable which doesn't even have a
sort-method; and somehow I find it unnatural to apply a 'sort' method
to an iterator whereas I find it perfectly natural to feed an iterator
to a function that does sorting for anything iterable...
Cheers,
--Tim
to throw fire on the fuel (:P), you can get the value back to an
in-place mutable change with a single expression...
mylist = [2,3,4,1]
print mylist.sort() or mylist
might not be too pythonic or maybe it is. I guess depends on what side
of the glass you might wish to view the solution :)
Lawrence Oluyede wrote: "an****@gmail.com" <an****@gmail.com> writes:
However, I wonder why L.sort() don't return the reference L, the performance of return L and None may be the same.
It's not "the same". sort() does not return anything.
Yes it does : it returns the None object. Why? I've just explained to you and so the others: by default operations on mutable objects are in place.
this is pure non-sens :
class MyList(list):
def sort(self):
return sorted(self)
This is a mutable object, and the sort() is not in place.
class MyObj(object):
def __init__(self, name):
self.name = name
def sayHello(self):
return "hello from %s" self.name
This is another mutable object, and I fail to see how 'in place' could
sensibly have any meaning when applied to sayHello().
Also, and FWIW, the fact that a method modifies the object it's called
on doesn't technically prevent it from returning the object:
class MyOtherList(list):
def sort(self, *args, **kw):
list.sort(self, *args, **kw)
return self
s = "abc" s.upper()
does return another string. String are immutable references.
Strings are immutable *objects*.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
vbgunz wrote: to throw fire on the fuel (:P), you can get the value back to an in-place mutable change with a single expression...
mylist = [2,3,4,1] print mylist.sort() or mylist
might not be too pythonic or maybe it is. I guess depends on what side of the glass you might wish to view the solution :)
Anyway, please do us a favor : avoid using such a thing in production
code !-)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jess Austin |
last post by:
hi,
I like the way that Python does lists, and I love the way it does
iterators. But I've decided I don't like what it does with iterators
of lists. Lists are supposed to be mutable sequences,...
|
by: Christopher Jeris |
last post by:
I am relatively new to JavaScript, though not to programming, and
I'm having trouble finding the idiomatic JS solution to the following
problem.
I have a table with (say) fields f1, f2, f3. I...
|
by: Nenad Jalsovec |
last post by:
using std::list;
struct Something{
Something( val ): value( val ){}
bool operator<( Something & s ){ return value < s.value; }
int value;
};
main(){
list< Something * > things;
|
by: Michael M. |
last post by:
How to find the longst element list of lists?
I think, there should be an easier way then this:
s1 =
s2 =
s3 =
if len(s1) >= len(s2) and len(s1) >= len(s3):
sx1=s1 ## s1 ist längster
|
by: apotheos |
last post by:
I can't seem to get this nailed down and I thought I'd toss it out
there as, by gosh, its got to be something simple I'm missing.
I have two different database tables of events that use different...
|
by: David Longnecker |
last post by:
I'm working to create a base framework for our organization for web and client-side
applications. The framework interfaces with several of our systems and provides
the business and data layer...
|
by: Zytan |
last post by:
This returns the following error:
"Cannot modify the return value of
'System.Collections.Generic.List<MyStruct>.this' because it is
not a variable"
and I have no idea why! Do lists return copies...
|
by: james_027 |
last post by:
hi,
are there available library or pythonic algorithm for sorting a list
of list depending on the index of the list inside the list of my
choice?
d_list = ,
,
,
,
|
by: Shawn Minisall |
last post by:
I'm writing a game that uses two functions to check and see if a file
called highScoresList.txt exists in the main dir of the game program.
If it doesn, it creates one. That part is working fine. ...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |