0

Lets say I have a list of strings with different method names, lets consider one-element list:

list = [split]

Is there any way to fetch the method name from the list when I want to use it? I'll give an exmple below, which doesn't work for me:

string = 'This is a string'
splitted_string = string.list[0]()

When I run the code, it's giving me:

AttributeError: 'str' object has no attribute 'list'

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
ruza22
  • 5
  • 1
  • "Is there any way to fetch the method name" - yes, `list[0].__name__`. No, it's not possible to call it with that syntax. – ForceBru Sep 20 '20 at 17:39
  • 1
    Can you clarify what you have and what you want to do? What is ``split``? Did you mean ``list = ["split"]``? Do you want to call ``string.split()``, where the ``split`` is read from ``list``? – MisterMiyagi Sep 20 '20 at 17:42
  • I'm trying to deal with a problem where the input I'm given contains a names of different methods. I've been able to extract those methods and put them in the list: `methods = ['intersection_update', 'update', 'symmetric_difference_update', 'difference_update']` Then I have a set A and a nested list called elements like so: `elements = [['2', '3', '5', '6', '8', '9', '1', '4', '7', '11'], ['55', '66'], ['22', '7', '35', '62', '58'], ['11', '22', '35', '55', '58', '62', '66']]` I was hoping that I could write a for loop that would apply a method and a corresponding sublist of elements – ruza22 Sep 20 '20 at 18:29
  • Are you aware that methods are first class? You can put in a list and iterate over them directly. – MisterMiyagi Sep 20 '20 at 20:27
  • I've already solved the problem, the getattr function was the key for me. Thanks for the help! – ruza22 Sep 21 '20 at 01:02

0 Answers0