Fredrik Lundh wrote:
That *is* a bad idea, and no, I didn't realize that would be the result. From the "mapping types" page:Jeremy Jones wrote:################################# file_dict = {}a_list = [("a", "a%s" % i) for i in range(2500)] b_list = [("b", "b%s" % i) for i in range(2500)] c_list = [("c", "c%s" % i) for i in range(2500)] d_list = [("d", "d%s" % i) for i in range(2500)] joined_list = a_list + b_list + c_list + d_list for key, value in joined_list: outfile = file_dict.setdefault(key, open("%s.txt" % key, "w"))you do realize that this opens the file again every time, so you end up having 4x2500 file handles pointing to 4 physical files. that's a bad idea.
I took this to mean that setdefault was a short-circuit and only created the default object once if the dictionary didn't contain the specified key. But I guess it's *me* who is passing setdefault a new file handle a few thousand times :-) Ouch. Thanks (and thanks to you, Duncan, for your reply as well),if you replace outfile = file_dict.setdefault(key, open("%s.txt" % key, "w")) with outfile = file_dict.get(key) if outfile is None: file_dict[key] = outfile = open("%s.txt" % key, "w") or, if you prefer, try: outfile = file_dict[key] except KeyError: file_dict[key] = outfile = open("%s.txt" % key, "w") your code won't depend on any undefined behaviour, and will work properly on all platforms. </F> Jeremy |
-- http://mail.python.org/mailman/listinfo/python-list