Re: [Tutor] What's the difference between sort(aList) and aList.sorted()

2017-07-27 Thread Alan Gauld via Tutor
On 27/07/17 03:22, C W wrote:
> Thank you very much, Steve!
> 
> I think I got it. To get help() on a method, you have to somehow invoke an
> object first.

Or just use the class. In Steven's examples he included list.sort.
'list' is the class name for a list.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the difference between sort(aList) and aList.sorted()

2017-07-27 Thread C W
Thank you very much, Steve!

I think I got it. To get help() on a method, you have to somehow invoke an
object first.

In your example, even an empty vector [] will do.

Thanks!

On Wed, Jul 26, 2017 at 10:16 PM, Steven D'Aprano 
wrote:

> On Wed, Jul 26, 2017 at 10:03:59PM -0400, C W wrote:
> > Thank you very much, all!
> >
> > One other question: how do you look up a method?
>
> Any of these will work:
>
> help(list.sort)
>
> help([].sort)
>
> alist = [1, 2, 3, 99]
> help(alist.sort)
>
>
>
>
> --
> Steve
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the difference between sort(aList) and aList.sorted()

2017-07-27 Thread C W
Thank you very much, all!

One other question: how do you look up a method?

>help(sort)
Traceback (most recent call last):

  File "", line 1, in 
help(sort)

NameError: name 'sort' is not defined


Back to function vs method, I came from R:

aList = sort(aList)

There was never aList.sort(), I was fine with it for years. I suppose
sort(aList) is more of a data science thing.

Thanks to all!


On Wed, Jul 26, 2017 at 8:21 PM, Steven D'Aprano 
wrote:

> On Wed, Jul 26, 2017 at 02:40:17PM -0400, C W wrote:
>
> > sorted(aList)
> > > [2, 3, 4, 5]
>
> sorted() makes a copy of whatever you give it, as a list, and sorts the
> copy. It doesn't have to be a list to start with:
>
> py> sorted("alphabet")
> ['a', 'a', 'b', 'e', 'h', 'l', 'p', 't']
>
>
> > aList.sort()
> > aList
> > > [2, 3, 4, 5]
>
> The sort() method only works on actual lists, and it sorts the list in
> place, just as list.reverse() reverses the list, list.append() appends a
> value to the list, list.insert() inserts a value into the list, etc.
>
> > Why is there both? They do the same thing. Is if I unknowingly hit the
> > keyboard with the aList.sort(), then the "damage" is permanent.
>
> They don't do the same thing. sorted() makes a copy of the argument
> first, list.sort() does not.
>
> We have both because sometimes one is useful and other times the other
> is useful. Originally, and for many years, Python only had list.sort(),
> and if you wanted to sort a copy you had to write:
>
> blist = list(alist)
> blist.sort()
>
> which is always a minimum of two lines and not very convenient. So it
> was eventually decided to add sorted().
>
>
>
> --
> Steve
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the difference between sort(aList) and aList.sorted()

2017-07-26 Thread Steven D'Aprano
On Wed, Jul 26, 2017 at 10:03:59PM -0400, C W wrote:
> Thank you very much, all!
> 
> One other question: how do you look up a method?

Any of these will work:

help(list.sort)

help([].sort)

alist = [1, 2, 3, 99]
help(alist.sort)




-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the difference between sort(aList) and aList.sorted()

2017-07-26 Thread Steven D'Aprano
On Wed, Jul 26, 2017 at 02:40:17PM -0400, C W wrote:

> sorted(aList)
> > [2, 3, 4, 5]

sorted() makes a copy of whatever you give it, as a list, and sorts the 
copy. It doesn't have to be a list to start with:

py> sorted("alphabet")
['a', 'a', 'b', 'e', 'h', 'l', 'p', 't']


> aList.sort()
> aList
> > [2, 3, 4, 5]

The sort() method only works on actual lists, and it sorts the list in 
place, just as list.reverse() reverses the list, list.append() appends a 
value to the list, list.insert() inserts a value into the list, etc.

> Why is there both? They do the same thing. Is if I unknowingly hit the
> keyboard with the aList.sort(), then the "damage" is permanent.

They don't do the same thing. sorted() makes a copy of the argument 
first, list.sort() does not.

We have both because sometimes one is useful and other times the other 
is useful. Originally, and for many years, Python only had list.sort(), 
and if you wanted to sort a copy you had to write:

blist = list(alist)
blist.sort()

which is always a minimum of two lines and not very convenient. So it 
was eventually decided to add sorted().



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the difference between sort(aList) and aList.sorted()

2017-07-26 Thread Alan Gauld via Tutor
On 26/07/17 19:40, C W wrote:

> My understanding of each is:
> 1) function(variable) is manipulating a vector, I can do bList =
> sorted(aList)
> 2) object.method() is permanently changing it, I don't even need to assign
> it in #1.
> 
> Why is there both? They do the same thing.

As you have just shown they do very different things.
One sorts the list in place the other returns a sorted
copy of the list.

Sometimes you want one, sometimes the other.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the difference between sort(aList) and aList.sorted()

2017-07-26 Thread Joel Goldstick
On Wed, Jul 26, 2017 at 2:40 PM, C W  wrote:

> Dear Python experts,
>
> I suppose I have the following Python code:
>
> aList = [3, 5, 2, 4]
>
> sorted(aList)
> > [2, 3, 4, 5]
>
> aList.sort()
>
> aList
> > [2, 3, 4, 5]
>
> My understanding of each is:
> 1) function(variable) is manipulating a vector, I can do bList =
> sorted(aList)
> 2) object.method() is permanently changing it, I don't even need to assign
> it in #1.
>
> Why is there both? They do the same thing. Is if I unknowingly hit the
> keyboard with the aList.sort(), then the "damage" is permanent.
>
> Thank you!
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>

I haven't looked it up recently, but one sorts in place (object.method) i
think, and the other  (a function) returns a new sorted list

-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] What's the difference between sort(aList) and aList.sorted()

2017-07-26 Thread C W
Dear Python experts,

I suppose I have the following Python code:

aList = [3, 5, 2, 4]

sorted(aList)
> [2, 3, 4, 5]

aList.sort()

aList
> [2, 3, 4, 5]

My understanding of each is:
1) function(variable) is manipulating a vector, I can do bList =
sorted(aList)
2) object.method() is permanently changing it, I don't even need to assign
it in #1.

Why is there both? They do the same thing. Is if I unknowingly hit the
keyboard with the aList.sort(), then the "damage" is permanent.

Thank you!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor