adding attributes to a function, from within the function

Michele Simionato mis6 at
Mon Nov 3 18:04:29 CET 2003

Fernando Rodriguez <frr at> wrote in message news:<as15qvs0nna5mvio4agd5su99fmrcgui4e at>...
> Hi,
> I'd like to do something like this:
> def fn():
>   error = "Error message"
>   return 1
> print fn.error

You can always use a closure:

  def function_with_error_message(error):
      def _(): return 1
      return _

  fn=function_with_error_message("Error message")
  print fn.error

However, IMHO using a class is better since automatic
documentation tools and inspection features work better for classes
than for closures. 


More information about the Python-list mailing list