Steven D'Aprano wrote:
The first question to ask is why are you using an array to hold multipleOn Fri, 04 May 2007 07:55:25 -0700, Alex Martelli wrote:What about the case where I have an array of objects that represent some particular binary file format. If the object is a file, then I want to copy its contents. If the object is a string, then I want to write the string. And so forth."Type-switching" in this way is a rather dubious practice in any language (it can't respect the "open-closed" principle).What do people think about functions that accept either a file name or a file object?def handle_file(obj): if type(obj) == str: need_to_close = True obj = file(obj, 'r') else: need_to_close = False do_something_with(obj.read()) if need_to_close: data.close() Good idea? Bad idea? Just a matter of personal preference? unidentified objects? Why not use a dictionary instead. You could add items using the object as key and type as data element. import typesThen you can iterate over the dictionary; selection/action by type. The question seems kind of bogus anyway. The only language feature that stores different elements in a single body are similar to 'C/C++' structures. At which point one has to ask... why not use the struct module? Ah ... perhaps the questioner didn't know about them? Well I'd suggest, before more 'annoyances' postings to review the top level of the module index @ http://docs.python.org/modindex.html. I find it a great place to start looking for python language features that my programs need, but I don't know, yet. sph -- HEX: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 |
-- http://mail.python.org/mailman/listinfo/python-list