title: A better way to call Class Methods from List of Class Objects - Python
body: <p>I was working on Python Classes and found that one cannot call class method from class object by looping through a list of such objects. Below is a sample code:</p>
  
  <pre><code>def show_student_details(*s_list):
      for s in s_list:
          print("Roll Number: ", s.get_roll_no())
          print("Name: ", s.get_name())
          print("Phone: ", s.get_phone())
          print("Marks: ", s.get_marks())
  </code></pre>
  
  <p>The code for Student class is: </p>
  
  <pre><code>class Student:
  
      def __init__(self, roll_no=0, name="", phone="", marks=-1):
          self.__roll_no = roll_no
          self.__name = name
          self.__phone = phone
          self.__marks = marks
  
      def get_roll_no(self):
          return self.__roll_no
  
      def get_name(self):
          return self.__name
  
      def get_phone(self):
          return self.__phone
  
      def get_marks(self):
          return self.__marks
  </code></pre>
  
  <p>Running this piece of code by passing some objects of Student class gives the following error:</p>
  
  <pre><code>  File "main.py", line 88, in &lt;module&gt;
      show_student_details(students)
    File "main.py", line 12, in show_student_details
      print("Roll Number: ", s.get_roll_no())
  AttributeError: 'list' object has no attribute 'get_roll_no'
  </code></pre>
  
  <p>The thing I understand is that <code>List</code> is itself a Class and Python interprets this code as if I was calling the <code>get_roll_no()</code> function on List Object. </p>
  
  <p>I googled this problem and found that <code>map()</code> and <code>methodcaller()</code> can be used to call the class methods but they didn't work for me.</p>
  
  <p>I know that this question has been asked multiple times on StackOverflow but I think none of them solved my problem which is '<strong><em>calling multiple class methods from object by selecting objects one by one from a list of class objects.</em></strong>'</p>
  
  <p>Any help will be appreciated. Thanks in Advance.</p>