0

From a previous question functions cannot be defined inline in a dictionary object.

However, I just have this simple case,

def getExtension(fileName):
    return os.path.splitext(fileName)[1]

funcDict = { 
    'EXT': getExtension,  # maybe os.path.splitext[1]?
    }

print(funcDict['EXT']('myFile.txt'))

Is there someway to specify that I only want one element of the tuple returned by os.path.splitext so I can get out of defining getExtension?

cheezsteak
  • 2,731
  • 4
  • 26
  • 41

2 Answers2

2
funcDict = {
    'EXT': (lambda x: os.path.splitext(x)[1])
}
Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64
1

I this it's sometimes better to use keyword arguments to instantiate a new dict, as the keyword arguments seem more readable.

funcDict = dict(EXT=(lambda x: os.path.splitext(x)[1]))

which will return:

{'EXT': <function <lambda> at 0x00000000021281E0>}
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331