FAQ
Hi all,

I'm learning Python with Google's Python class

Ik have a question about the following code:
=================================
def sort(var):
return var[-1] #returns the last character of var

def sort_last():
tup = [(1, 3), (3, 2), (2, 1)]
print(sorted(tup, key=sort))

sort_last()
==================================

I uderstand everything except (var) value.
I understand that key=sort calls the sort function.
But where comes the var value from?
Does sort automatic pass the value from tup to (var)

because (var) is nowhere defined.

Thanks,

Martin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110125/9dce4c8a/attachment.html>

Search Discussions

  • Marc Tompkins at Jan 26, 2011 at 12:37 am

    On Tue, Jan 25, 2011 at 11:46 AM, It't me wrote:
    Hi all,

    I'm learning Python with Google's Python class

    Ik have a question about the following code:
    =================================
    def sort(var):
    ?return var[-1]? #returns the last character of var

    def sort_last():
    ? tup = [(1, 3), (3, 2), (2, 1)]
    ? print(sorted(tup, key=sort))

    sort_last()
    ==================================

    I uderstand everything except (var) value.
    I understand that key=sort calls the sort function.
    But where comes the var value from?
    Does sort automatic pass the value from tup to (var)

    because (var) is nowhere defined.

    Thanks,

    Martin
    Could you post the URL to that class? 'Cause I don't see it either,
    from what you've posted. If that's all there is, something is
    profoundly wrong.


    --
    www.fsrtechnologies.com
  • Emile van Sebille at Jan 26, 2011 at 12:53 am
    On 1/25/2011 11:46 AM It't me said...
    Hi all,

    I'm learning Python with Google's Python class

    Ik have a question about the following code:
    =================================
    def sort(var):
    return var[-1] #returns the last character of var
    Nit: it'll be a character if passed in a string of characters. If you
    pass in a tuple or list, it'll be the last thing in the list.
    def sort_last():
    tup = [(1, 3), (3, 2), (2, 1)]
    print(sorted(tup, key=sort))

    sort_last()
    ==================================

    I uderstand everything except (var) value.
    I understand that key=sort calls the sort function.
    But where comes the var value from?
    Does sort automatic pass the value from tup to (var)

    because (var) is nowhere defined.
    in the line

    def sort(var):

    var becomes in effect a placeholder for whatever value is passed to the
    function.

    in the line

    print(sorted(tup, key=sort))

    sort is being passed into sorted, and looking into the python
    documentation for sorted we find two things.



    sorted(iterable[, cmp[, key[, reverse]]])?

    Return a new sorted list from the items in iterable.



    1) that sorted expects the first paramater to be iterable. And a bit
    further we find...



    key specifies a function of one argument that is used to extract a
    comparison key from each list element: key=str.lower. The default value
    is None (compare the elements directly).


    Aahh... there it is -- each element of the iterable will be passed into
    the function named by key to determine the comparison key.


    Now, applies to your code this means that for each pair in
    [(1, 3), (3, 2), (2, 1)] sorted will comare the result of your sort
    function when passed that pair, and your sort function returns the last
    element in that pair.


    HTH,

    Emile
  • Steven D'Aprano at Jan 26, 2011 at 4:55 am

    It't me wrote:
    Hi all,

    I'm learning Python with Google's Python class

    Ik have a question about the following code:
    =================================
    def sort(var):
    return var[-1] #returns the last character of var
    Why is the function called "sort" when it doesn't sort?

    Why not call it "run" or "cook" or "pink" or "xhg6gf2jgf"?

    Function names *must* tell you what they do, or at least give you a
    hint. Misleading names like "sort" for something that doesn't sort is
    very bad. Even a name like "xhg6gf2jgf" would be better than a name that
    lies about what it does.

    def sort_last():
    tup = [(1, 3), (3, 2), (2, 1)]
    print(sorted(tup, key=sort))

    sort_last()
    ==================================

    I uderstand everything except (var) value.
    I understand that key=sort calls the sort function.
    Do you mean the sort function you wrote, or the built-in list sort
    method? I'll assume you mean the sort function you wrote.

    But where comes the var value from?
    Does sort automatic pass the value from tup to (var)
    No. sorted() passes the values to the key function "sort". "sort"
    *receives* those values, it doesn't go and get them.

    When you write a function with an argument, the argument isn't defined
    until you call it:


    def add_one(x):
    return x + 1


    x is a local variable of the function, but is not yet defined. But when
    you call the function:

    add_one(42)

    x is set to 42.


    In your "sort" function, the local variable is called "var". Calling

    sorted(tup, key=sort)

    with tup = [(1, 3), (3, 2), (2, 1)] results in the sorted() function
    calling your key function "sort" with each of the items in turn:

    * build a new list by calling the "sort" function with (1,3), then
    (3,2), then (2, 1)

    * sort a copy of the original list according to the values in the new list

    * return the sorted copy



    --
    Steven

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouptutor @
categoriespython
postedJan 25, '11 at 7:46p
activeJan 26, '11 at 4:55a
posts4
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase