Extending module
I'm getting really sick of having to add_type
a bunch of 'missing' mimetypes when using the 开发者_C百科mimetypes
module, is there someway I can do the following?
# This file is named mimetypes.py
from mimetypes import *
add_type('application/x-rar-compressed', 'rar', False)
add_type('video/mp4', 'mp4', False)
add_type('video/x-ms-wmv', 'wmv', False)
add_type('video/x-flv', 'flv', False)
The problem with this is that it tries to import itself, not the 'real' mimetypes
module.
I am aware it's unpythonic and bad practice, but I don't think it would really break anything.
Don't call your module mimetypes
.
That's it. Call it something else, and it works. Maybe extra_mimetypes
or something. Then just import extra_mimetypes
from your software and your father got a brother called Bob.
The only unpythonic thing with it is the from mimetypes import *
. Change that to from mimetypes import add_type
. This is because you don't need to extend mimetypes at all. The add_type()
call adds your new types to the mimetypes registry, no extension needed.
And in general, you don't need to extend any module. If you want to add extra functions that doesn't exist in a module, you can have those extra functions in a second module. Extending modules like you wanted to do is pretty much never necessary.
精彩评论