Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
replace if\else with loop?
#3
Of course. The standard library includes the operator module that has functions for those basic mathematical operations, e.g.

>>> import operator
>>> f = operator.add
>>> f(1, 2)
3
>>> f = operator.mul
>>> f(2, 3)
6
>>> 
It's really useful to be able to treat functions as values in the same way that, say, integers are values - being able to pass them around, or assign them to variables as I'm doing here.

Also, it sounds like having two separate lists is not what you want. Why not just have a dict that maps the symbol to the function instead?
Reply


Messages In This Thread
replace if\else with loop? - by mcmxl22 - Mar-09-2020, 07:03 AM
RE: replace if\else with loop? - by michael1789 - Mar-09-2020, 07:49 AM
RE: replace if\else with loop? - by ndc85430 - Mar-09-2020, 07:50 AM
RE: replace if\else with loop? - by buran - Mar-09-2020, 08:20 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace for loop to search index position illmattic 5 2,357 Sep-03-2022, 04:04 PM
Last Post: illmattic
  Beginner problem, replace function with for loop Motley_Cow 9 6,321 Sep-13-2019, 06:24 AM
Last Post: Motley_Cow
  Search & Replace - Newlines Added After Replace dj99 3 4,356 Jul-22-2018, 01:42 PM
Last Post: buran

Forum Jump:

User Panel Messages

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