bound member functions ?

Steve Holden sholden at holdenweb.com
Mon Oct 8 05:04:34 EDT 2001


"Achim Domma" <achim.domma at syynx.de> wrote ...
> Hi,
>
> I have tried the following code :
>
> --- code --------------------------
> class Dummy:
>     def __init__(self,val):
>         self.val = val
>
>     def mult(self,fac):
>         return self.val * fac
>
>
> obj = Dummy(5)
> mult = obj.mult
> print mult(5)
> ------------------------------------
>
> The script prins 25 as I expected. If I change now the last three lines to
>
> --- code ---------------------------
> ...
> mult = Dummy(7).mult
> print mult(5)
> -------------------------------------
>
> I still get 35. What's about reference counting in this case ? Is there a
> refernce hidden in mult which keeps 'Dummy(7)' alive ? Can I be sure, that
> the object constructed by Dummy(7) is not destroyed after this line ?
>
Yes. The reference to the bound method will keep the object around as long
as you need it.

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list