Python Forum
[split] Creating a variable as a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Creating a variable as a function
#21
Not least because in reality, those function calls may be expensive or have some side effect somewhere (writing to a database for example) that you only really want to do when you need to.
Reply
#22
You prefer it like i implemented it in post # 1 ?

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#23
it's not a matter of preference. the point is to NOT execute all functions while creating the dict
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
#24
Like so:
#the functions
def AAA():
    return 'AAA'

def BBB():
    return 'BBB'

def ELSE():
    return 'UNKNOWN'
# ---------------------------
#(1)single key dict with default
def switcher1(k):
    myDict = {100:AAA,200:BBB}
    return myDict.get(k, ELSE)

print(switcher1(200)())
print(switcher1(502)())
# -----------------------------
#(2)tuple with (optional) multiple keys and default
def switcher2(k):   
    myDict2 = {('a'):AAA, ('x', 'y', 'z'):BBB}
    myDict2 = {key:value for item, value in myDict2.items() for key in item}
    return myDict2.get(k, ELSE)

print(switcher2('z')())
print(switcher2('M')())
# --------------------------------
#(3) range dict with default
from range_key_dict import RangeKeyDict
 
def switcher3(k):
    myDict3 =  RangeKeyDict({(0,100): AAA,(101,200): BBB})
    return myDict3.get(k, ELSE)

print(switcher3(152)())
print(switcher3(5210)())
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 640 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 585 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,294 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 942 Aug-07-2023, 05:58 PM
Last Post: Karp
  Split string using variable found in a list japo85 2 1,300 Jul-11-2022, 08:52 AM
Last Post: japo85
  Retrieve variable from function labgoggles 2 1,045 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  Cant transfer a variable onto another function KEIKAS 5 1,890 Feb-09-2022, 10:17 PM
Last Post: deanhystad
  Please explain uncommon way of declaring and using variable [function.variable] esphi 4 2,332 Nov-07-2020, 08:59 AM
Last Post: buran
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,224 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  passing variable to function Rejoice 4 2,873 Sep-11-2020, 03:27 AM
Last Post: Pleiades

Forum Jump:

User Panel Messages

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