getting the current function

Jonathan Gardner jgardner.jonathangardner.net at gmail.com
Thu Sep 6 17:07:20 EDT 2007


On Sep 6, 8:43 am, Gary Robinson <gary... at mac.com> wrote:
>
> I welcome feedback of any type.
>

This all seems a bit too complicated. Are you sure you want to do
this? Maybe you need to step back and rethink your problem.

Your example can be rewritten a number of different ways that are
easier to think about.

The simple approach:

  _x_counter = 0
  def x():
      _x_counter += 1
      return _x_counter

The generator approach:

  def counter_gen():
      x = 0
      while True:
          x += 1
          yield x

  counter = counter_gen()
  counter.next()
  counter.next() # etc...

A class-based approach:

  class Counter(object):
      def __init__(self):
          self._x = 0

      def count(self):
          self._x += 1
          return self._x

In general, there really isn't a need to mess with stack frames unless
you are really interested in looking at the stack frame.




More information about the Python-list mailing list