Python Forum
You can call a function without it's arg's?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
You can call a function without it's arg's?
#1
Absolutely brand new to python.
Here is some code from a tutorial that works, I just don't understand why.
strs = ['xc', 'zb', 'yd' ,'wa']

def MyFn(s):
    return s[-1]

print(sorted(strs, key=MyFn))

## ['wa', 'zb', 'xc', 'yd']
My question is, why am I not required to pass an argument (s) to MyFn?
Why is the code *not*
print(sorted(strs, key=MyFn(strs)))
Reply
#2
sorted uses the function that is passed to extract a comparison key from each element in iterable,
it will call the function and fill in s itself.
If you were to call the function, the result of the called function would be given to sorted and it would then not work.
Reply
#3
So I can only use functions with a single argument? If my sort value required using 2+ arguments, I'd have to first assign a variable to the return value of the function, then refer to the variable in the sort method?

someVariable = MyFn(10, True, 'Fred')

print(sorted(strs, key=someVariable)
Reply
#4
Why would your sort value require 2+ arguments? The argument to the key function is the item being sorted. You just return a different value to be sort that item by.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
I'm apparently not being clear. I understand the simplistic example, the syntax, and how it works. What I am asking is what IF you have far more complicated code used to determine what your *single* value is to sort by. I am not asking for how to get 2+ sort values. I am asking how to refer to a function that determines one sort value which requires more than one argument.

sortKeyFunction(i, j, k):
    If i and j or not k:
        return [-1]
    else:
        return [0]
I am not asking about multiple return values. The solution would be easy if the syntax allowed
print(sorted(strs, key=sortKeyFunction(True, True, False)))
but it does not.
I can only assume that you have to assign a variable to the return value of the function and then use the variable in the sorted statement.
Reply
#6
As it is stated in the docs:
Quote:The value of the key parameter should be a function that takes a single argument and returns a key to use for sorting purposes.
note the bold text - it should be single argument function
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
And again I ask: why would you want to do this? Your example returns the same value for every item, so the list order doesn't change. It's pointless. I can't think of a practical reason to have more than one parameter to the key function. If you come up with a practical reason and a real example, we might be able to come up with a way around the restriction of one parameter for the key function. Without that, all we can say is "you can't do that."
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 795 May-02-2023, 08:40 AM
Last Post: Gribouillis
  how to call an object in another function in Maya bstout 0 2,082 Apr-05-2021, 07:12 PM
Last Post: bstout
  In this function y initially has no value, but a call to foo() gives no error. Why? Pedroski55 8 3,500 Dec-19-2020, 07:30 AM
Last Post: ndc85430
  Struggling for the past hour to define function and call it back godlyredwall 2 2,225 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  list call problem in generator function using iteration and recursive calls postta 1 1,914 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  function call at defined system time? Holon 5 3,235 Oct-06-2020, 03:58 PM
Last Post: snippsat
  How to call/read function for all elements in my list in python johnny_sav1992 1 2,083 Jul-27-2020, 04:19 PM
Last Post: buran
  Python: Call function with variabele? Ending in error. efclem 5 2,939 Apr-22-2020, 02:35 PM
Last Post: buran
  How to mock an object that is created during function call? Schlangenversteher 0 1,973 Jan-31-2020, 01:36 PM
Last Post: Schlangenversteher
  Is there a way to search for function call? mtran 2 2,271 Jan-14-2020, 02:07 AM
Last Post: mtran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020