Avoiding if..elsif statements

Daniel Nogradi nogradi at gmail.com
Fri Aug 25 18:54:12 EDT 2006


> Code:
>
> value = self.dictionary.get(keyword)[0]
>
> if value == "something":
>     somethingClass.func()
> elsif value == "somethingElse":
>     somethingElseClass.func()
> elsif value == "anotherthing":
>     anotherthingClass.func()
> elsif value == "yetanotherthing":
>     yetanotherthingClass.func()
>
> Is it possible to store these function calls in a dictionary so that I
> could just call the dictionary value?

How about (untested):

def x():
  print 'x'

def y():
  print 'y'

funcdict={ 'valuex': x, 'valuey': y }

funcdict['valuex']()



More information about the Python-list mailing list