[IronPython] Static variables (Python basics)

David Seruyange idmkid at yahoo.com
Sun Sep 30 06:15:15 CEST 2007


Hey everyone, 

I'm wondering how one caches things in python in a manner similar to how a static variable or class member might in C#.  I can only think of an equivalent in something like a global variable, but is there another? I'm not good enough yet to think in a manner "pythonic" but I've an inkling that a generator may do this?  This program (where I want to cache a list of factorials once computed) is what prompted my questions:

f = [1]    
    
def fact(n):
    if n == 1:return 1
    if(n > len(f)):
        for i in range(len(f), n+1):
            f.append(i * f[i -1])
    return f[n]
            
print fact(4) #set it up
print fact(3) #pulled from cache
print fact(6) #grow it 





       
____________________________________________________________________________________
Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
http://answers.yahoo.com/dir/?link=list&sid=396545469
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20070929/e42c2fba/attachment.html>


More information about the Ironpython-users mailing list