Python Forum
Defining multiple functions in the same def process
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Defining multiple functions in the same def process
#4
Is what you really want to do to write a generic function where the thing that varies is a function that's called inside? If so, look into the idea of higher order functions - in Python, functions are values and can be passed around like other things. A higher order function is one that takes another function as a parameter (or returns one, but the former case sounds more useful for you right now). An example of such a thing is the built in map function:

>>> xs = [1, 2, 3]
>>> list(map(lambda x: x * 2, xs))
[2, 4, 6]
>>> words = ["foo", "bar", "baz"]
>>> list(map(lambda w: w.upper(), words))
['FOO', 'BAR', 'BAZ']
map takes a function as its first argument and applies it to each item of the iterable passed as its second argument. In the two examples, I pass in two different functions: one that doubles the argument that I use with the list of ints and one that turns its argument to uppercase, which I use with the list of strings.
Reply


Messages In This Thread
RE: Defining multiple functions in the same def process - by ndc85430 - Aug-09-2020, 05:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Defining Functions theturtleking 4 2,886 Dec-07-2021, 06:45 PM
Last Post: deanhystad
  Run multiple process using subprocess Shiri 3 3,959 Nov-28-2021, 10:58 AM
Last Post: ghoul
  Process multiple pdf files Spartan314 1 1,363 Oct-27-2021, 10:46 PM
Last Post: Larz60+
  How to call multiple functions sequentially Mayo 2 9,544 Jan-06-2021, 07:37 PM
Last Post: Mayo
Question trouble with functions "def", calling/defining them Duck_Boom 13 4,523 Oct-21-2020, 03:50 AM
Last Post: Duck_Boom
  Multiple lambda functions in zipped list not executing psolar 0 1,628 Feb-13-2020, 12:53 PM
Last Post: psolar
  naming images adding to number within multiple functions Bmart6969 0 1,954 Oct-09-2019, 10:11 PM
Last Post: Bmart6969
  How to sharing object between multiple process from main process using Pipe Subrata 1 3,711 Sep-03-2019, 09:49 PM
Last Post: woooee
  Defining functions TommyAutomagically 1 1,914 Apr-25-2019, 06:33 PM
Last Post: Yoriz
  Multiple process access to a serial port mkonnov 0 3,092 Apr-14-2019, 12:42 PM
Last Post: mkonnov

Forum Jump:

User Panel Messages

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