<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def clear(self, shape=None):
 """Empties grid and sets shape to shape Clears all attributes, row heights, column withs and frozen states. Empties undo/redo list and caches. Empties globals. Properties shape: 3-tuple of Integer, defaults to None \tTarget shape of grid after clearing all content. \tShape unchanged if None """


        # Without setting this explicitly, the cursor is set too late
        self.grid.actions.cursor = 0, 0, 0
        self.grid.current_table = 0

        post_command_event(self.main_window.grid, self.GotoCellMsg,
                           key=(0, 0, 0))

        # Clear cells
        self.code_array.dict_grid.clear()

        # Clear attributes
        del self.code_array.dict_grid.cell_attributes[:]

        if shape is not None:
            # Set shape
            self.code_array.shape = shape

        # Clear row heights and column widths
        self.code_array.row_heights.clear()
        self.code_array.col_widths.clear()

        # Clear macros
        self.code_array.macros = ""

        # Clear caches
        self.code_array.result_cache.clear()

        # Clear globals
        self.code_array.clear_globals()
        self.code_array.reload_modules()