setting attributes on external types (was Re: eof)

samwyse samwyse at
Sat Nov 24 11:24:53 CET 2007

On Nov 23, 2:06 am, greg <g... at> wrote:
> There's a fair amount of overhead associated with providing
> the ability to set arbitrary attributes on an object, which
> is almost never wanted for built-in types, so it's not
> provided by default.
> You can easily get it if you want it by defining a Python
> subclass of the type concerned.

Speaking of which, I've got a big file:

>>> input = open('LoTR.iso')

I'd like to get the md5 hash of the file:

>>> import md5
>>> m =

I've also got this nifty standard module which will allow me, among
other things, to copy an arbitrary file:

>>> import shutil

I'd like to say copy the file to my object, but it doesn't quite work:

>>> shutil.copyfileobj(input, m)

Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    shutil.copyfileobj(source, m)
  File "C:\Python25\lib\", line 24, in copyfileobj
AttributeError: '_hashlib.HASH' object has no attribute 'write'

No problem, I'll just add an attribute:

>>> setattr(m, 'write', m.update)
Traceback (most recent call last):
  File "<pyshell#15>", line 1, in <module>
    setattr(m, 'write', m.update)
AttributeError: '_hashlib.HASH' object has no attribute 'write'

Anyone have an example of how to efficiently do this?  Thanks!

More information about the Python-list mailing list