Python Forum
passing an argument to avoid a global
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
passing an argument to avoid a global
#1
i have a sort key function that needs an int passed to it by the caller of sorted() that will effect how it sets up the key to be compared. i want to do this without using a global. anyone have an idea how?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Just create a new function:
sorted(iterable, key=lambda: key_function(x))
Reply
#3
it's a more complex multi-line function that will ultimately be an imported module.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
And how exactly does that stop you from wrapping it in a lambda and passing arguments to it?
Reply
#5
i don't know how to do multi-line nested conditions and loops in a lambda.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
In your original question you said you just need to pass an int to your function.
Where do "multi-line nested conditions and loops" come from? Has the problem changed since?
Reply
#7
the problem has not changed. it never was limited to one line. but i didn't know someone might assume it was.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
Maybe you could show an example of what you want to do?
At this point, I have trouble figuring out what the actual problem is.
Reply
#9
Something like this?
https://docs.python.org/3/library/operat...itemgetter

from functools import wraps


def int_itemgetter(index_or_key):
    """
    Is similar to operator.itemgetter, but it converts the element to an integer.
    """
    @wraps(int_itemgetter)
    def inner(element):
        return int(element[index_or_key])
    return inner

some_data = [(1,30,3), (4,20,6), (7,10,9)]
get_second = int_itemgetter(1)
print(sorted(some_data, key=get_second))
or written as class


class Itemgetter:
    def __init__(self, index_or_key):
        self._idx = index_or_key

    def __call__(self, element):
        return int(element[self._idx])

some_data = [(-1,30,3), (-4,20,6), (-7,10,9)]
get_first = Itemgetter(0)
print(sorted(some_data, key=get_first))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#10
i still don't understand decorators so i'm looking at DeaD_EyE's 2nd example. suppose the sort key function is a function where i am padding those strings from re.split() that are decimal strings. there needs to be a consistent length of the padding across all records. the code that calls sorted(...,key=) would set up that length in a way that they key function could get it. then it would pad those decimal strings so they all are consistent. is that what the argument is for in line 9? i don't follow what lines 5 and 6 are doing.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Passing Argument Errors cosmarchy 6 2,430 Sep-29-2021, 06:09 AM
Last Post: snippsat
  Passing argument from top-level function to embedded function JaneTan 2 2,236 Oct-15-2020, 03:50 PM
Last Post: deanhystad
  SyntaxError: positional argument follows keyword argument syd_jat 3 5,820 Mar-03-2020, 08:34 AM
Last Post: buran
  Passing an argument by reference Exsul 12 4,699 Aug-22-2019, 07:29 AM
Last Post: DeaD_EyE
  Global variable does not seem to be global. Columbo 6 3,672 Jul-15-2019, 11:00 PM
Last Post: Columbo
  help with threading module and passing global variables ricardons 1 7,837 Feb-21-2019, 12:48 PM
Last Post: stullis
  Is This Code Ok? How Can I Avoid Using Global Variables? digitalmatic7 9 5,790 Feb-15-2018, 09:32 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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