Dynamic methods and lambda functions

coutinhotiago at gmail.com coutinhotiago at gmail.com
Wed Jan 28 13:05:39 EST 2009


Hi!

  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

  Cheers,
Tiago



More information about the Python-list mailing list