How to derive a class from md5?

Shalabh Chaturvedi shalabh at cafepy.com
Wed Jul 14 02:20:35 EDT 2004


Josef Wolf wrote:
> Hello!
> 
> I'd like to create a class to calculate md5's of a file.  So I thought, I'd
> derive from the md5 class.  I came up with following code:
> 
>   #!/usr/bin/python
> 
>   import md5
> 
>   class md5file(md5):
>       def __init__(self, filename):
>           md5.__init__(self)
>           f=file(filename,"r")
>           for l in f:
>               self.update(l)
>           f.close()
> 
>   print md5file("/etc/passwd").hexdigest()
> 
> But running this gives me the following error:
> 
>   Traceback (most recent call last):
>     File "py/t.py", line 5, in ?
>       class md5file(md5):
>   TypeError: function takes at most 2 arguments (3 given)
> 
> dir(md5) shows that md5 don't have an __init__() method so I tried to use
> its new(), with the same result.  What am I doing wrong here?
> 

Why subclass? You could just get the md5 object by doing:

from md5 import md5
m = md5(open(filename).read())

Simple is better than complex.

--
Shalabh




More information about the Python-list mailing list