On Fri, Oct 04, 2002 at 08:41:42AM -0700, Bob Miller wrote:
>Those of you who were at the clinic last night know that I
>was asking for help on a weird limitation of Python.
>
>The problem:  Consider the function, foo(), in this C program.

I don't fully understand your objection to making a callable class, as
that's probably how I'd do it.  You were saying that it returned an
object instead of the value...  I just look at it as having a class that
implements the functionality that you want and you create an instance of
that class.

   >>> class fooClass:
   ...    def __init__(self):
   ...       self.n = 0
   ...    def __call__(self):
   ...       self.n += 1
   ...       return(self.n)
   ... 
   >>> foo = fooClass()
   >>> print foo(), foo()
   1 2

However, another way to go about that is to set up a default argument
which is a mutable object:

   >>> def foo(n = [0]):
   ...    n[0] += 1
   ...    return(n[0])
   ... 
   >>> print foo(), foo()
   1 2

I personally prefer the class mechamism because it's more obvious what's
going on and less likely to produce misunderstsanding.  You seem
hell-bent on something more like the latter though.  ;-)

Sean
-- 
 Well, what does she expect? You leave your navigator lying around,
 naturally somebody is going to run over him.  -- _Death_Race_2000_
Sean Reifschneider, Inimitably Superfluous <[EMAIL PROTECTED]>
tummy.com, ltd. - Linux Consulting since 1995.  Qmail, Python, SysAdmin
_______________________________________________
Eug-LUG mailing list
[EMAIL PROTECTED]
http://mailman.efn.org/cgi-bin/listinfo/eug-lug

Reply via email to