Listing functions in a file IN ORDER

Duncan Grisby duncan-news at
Wed Jun 30 16:43:06 CEST 2004

In article <mailman.278.1088520506.27577.python-list at>,
Ian Sparks <Ian.Sparks at> wrote:

>I have a python file with a number of functions named with the form doX so :
>The order these are executed in is important and I want them to be
>executed top-down. They all have the same parameter signature so I'd
>like to do :

All you need to do is sort them by line number:

  from types import FunctionType

  def linecompare(a,b):
      return cmp(a.func_code.co_firstlineno, b.func_code.co_firstlineno)

  func_list = [ f for (n,f) in vars(mymodule).items()
                if isinstance(f, FunctionType) and n.startswith("do") ]


Notice that I looked just for functions since other callables like
classes don't have the right attributes.



 -- Duncan Grisby         --
  -- duncan at     --
   -- --

More information about the Python-list mailing list