-1

How can I call a (string) method assigned to a dict value like this

dict = { True: ''.lower }

I tried

flip = True
print("A".dict[flip])

but then I get:

AttributeError: 'str' object has no attribute 'dict'
  • Pass the string value to the method, by calling that method: `dict[flip]("A")`. You want to store *unbound methods* however, e.g. `str.lower` instead of `''.lower`. The latter is already bound, to the empty string. – Martijn Pieters Aug 19 '22 at 18:25

1 Answers1

1
d = {True: lambda x: x.lower()}
flip = True
print(d[flip]('A'))

prints

a
Michael Hodel
  • 2,845
  • 1
  • 5
  • 10