Working program but review required to make it better

Hi All,

I just started learning Python using online resources and am just a couple of days old in it. I have quite a good experience in Java though (10+ years) and Python caught my fancy during the Christmas holidays as had nothing better to do

I wrote a quick and dirty code to compute factorial of a number using Python and I need an expert's opinion about it with respect to the structure and semantics - is there something that can be more optimized in it, something that was done incorrectly etc. Am interested to learn the language right the first time, and will immensely appreciate any feedback.

The idea of code is simple - being a recursive algorithm, I've tried to cache all previously computed values of factorial in a file which is cached when an object of FactCache is created. Factorial then uses the cache to reduce recursion.

Code follows:

Code:

import os
import pickle
"""
Caches and pickles factorial of a number.
Upon instantiation, sarches for the file 'fact.cache'
in the current directory or in the path passed during
construction. If not found then creates the file there.
If found then unpickles it and creates the dictionary
with pre-calculated factorial values.
"""
class FactCache:
c = {}
def __init__(self,path="./fact.cache"):
self.path=path
try:
if(os.path.isfile(self.path)):
print("Cache file found. Reading it now...")
with open(self.path,'rb') as readCache:
self.c = pickle.load(readCache)
else:
print("File not found. Creating it now...")
with open(self.path,'wb') as writeCache:
pickle.dump(self.c,writeCache)
except IOError as err:
print("Oops! Factorial cache not loaded!",str(err))
pass
finally:
print(self.c)
"""
Stores the value (f) of the factorial for a number (n) in the file.
"""
def add(self,n,f):
self.c[n]=f
with open(self.path,'wb') as writeCache:
pickle.dump(self.c,writeCache)
"""
Returns the value of the factorial for a number (n) from the dictionary.
"""
def get(self,n):
if n in self.c:
return self.c[n]
else:
return -1
"""
Computes factorial of a given number and
reuses all previous results to reduce recursion.
"""
def factorial(n,c):
f = c.get(n)
if(f == -1):
if(n==0):
return 1
else:
v = n*factorial(n-1,c)
c.add(n,v)
return v
else:
print("Value served from cache for ",n)
return f