Executing a function's flow My __main__.py file has a function's flow, based on methods of each class instance, which each one has its own methods and attributes, like this:

from xlrd                              import open_workbook
from pprint                            import pprint
from rubrica_manager                   import RubricaManager
from rectified_rubrica_manager         import RectifiedRubricaManager
from rectification_spreadsheet_manager import RectificationSpreadsheetManager
from gps_manager                       import GPSManager
from sys                               import exit
from data_comparison                   import DataComparison

def main():
    '''
    Execute PostDiagnosisChecker
    '''
    rm  = RubricaManager("File1.xlsx")
    rrm = RectifiedRubricaManager("File2.xlsx")
    rsm = RectificationSpreadsheetManager("File3.xlsx")
    gm  = GPSManager("File4.xlsx")

    object_collection = [rm, rrm, rsm, gm]

    for current_spreadsheet in object_collection:
        '''
        evaluate each object if it has the method or attribute before executing
        '''
        methods_list = dir(current_spreadsheet)
        if "load_data" in methods_list.get("load_data"):
            current_spreadsheet.load_data()
        if "spreadsheet_to_array" in methods_list:
            current_spreadsheet.spreadsheet_to_array()
        if "headers_array" in methods_list:
            current_spreadsheet.headers_array
        if "headers_used_indexes" in methods_list:
            current_spreadsheet.get_used_indexes(0)
        if "remove_duplicity_by_timestamp" in methods_list:
            current_spreadsheet.remove_duplicity_by_timestamp()
        if "array_to_dictionary" in methods_list:
            current_spreadsheet.array_to_dictionary()


There are methods commonly to all of instances, some of them are not. Because of that, I thought about a way to evaluate if each instance has its respective method, but I don't know if this is the most performative way. Any ideas?

Python version 2.6.
You've got a random string inside your function, it is unhelpful, you should remove that. If, however, you meant to make a comment, you should have used the comment sign (#).

For the existence of methods, combining dir and if .. in is inneficient as lists lookups are \$\mathcal{O}(n)\$ but attribute access is usually done through an instance's __dict__. You have two possibilities: either you execute it nonetheless and handle the AttributeError if it didn't exist, or use the getattr function to retrieve the method and then you execute it. The advantages of the latter being that:


You can easily encapsulate that behaviour in a function;
You can use the third parameter of getattr to provide a default value in case the attribute doesn't exist.


Something like this should be very generic, remove functionalities as you don't need them:

def do_nothing(*args, **kwargs):
    pass

def execute_method(name, instance, *args, **kwargs):
    method = getattr(name, instance, do_nothing)
    return method(*args, **kwargs)


Going through the rest of the code, you may also allow to pass the names of the files as parameters.

Proposed improvements look like:

FILES_TYPES = [
    RubricaManager,
    RectifiedRubricaManager,
    RectificationSpreadsheetManager,
    GPSManager,
]
METHODS = [
    'load_data',
    'spreadsheet_to_array',
    'headers_array',
    'get_used_indexes',
    'remove_duplicity_by_timestamp',
    'array_to_dictionary',
]


def do_nothing(*args, **kwargs):
    pass


def execute_method(name, instance, *args, **kwargs):
    method = getattr(name, instance, do_nothing)
    return method(*args, **kwargs)


def main(filenames=('File1.xlsx', 'File2.xlsx', 'File3.xlsx', 'File4.xlsx')):
    for filename, cls in zip(filenames, FILES_TYPES):
        spreadsheet = cls(filename)
        for method_name in METHODS:
            execute_method(method_name, speadsheet)