Dynamic methods and lambda functions

coutinhotiago at gmail.com coutinhotiago at gmail.com
Wed Jan 28 19:05:39 CET 2009


  I had the same problem myself.
  Mark's detailed explanation really helped me understand.

  I ended up doing something like:

  class A:
    def __init__(self):
      names = 'n1', 'n2'
      for n in names:
        setattr(self, "get%s" % n, self._createGetter(n))

    def _createGetter(self, n):
      def f(): return n
      return f

  Thanks a lot


More information about the Python-list mailing list