adding attributes to a function, from within the function

Michele Simionato mis6 at pitt.edu
Mon Nov 3 12:04:29 EST 2003


Fernando Rodriguez <frr at easyjob.net> wrote in message news:<as15qvs0nna5mvio4agd5su99fmrcgui4e at 4ax.com>...
> 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
      _.error=error
      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. 

                         Michele




More information about the Python-list mailing list