Return to Home Page
      Blog     Consulting     Seminars     Calendar     Books     CD-ROMS     Newsletter     About     FAQ      Search
 

When speaking of types, as you do much further up, you mention lists and associative arrays. It might be well worthwhile to mention tuples, and the differences between them and lists.

Specifically, tuples cannot be modified (as you mention above), and are generally significantly faster than lists. Thus, premature optimization aside, choosing to use tuples rather than lists can be important.

On the other hand, it might be an interesting exercise in optimization to go through your Python project's code and determine if tuples could be used in places where lists currently exist. I wonder if there is a Python profiler that takes this optimization into account? If not, there probably should be... not that I have the time to write it right now, and it might be better off going into the interpreter.

f  e  x


"""This %s is very nice %s because it is 40%% proof""" % ('whisky', 'to drink') illustrates how to escape '%' and escapes trying \.

f  e  x


I tried by hand with:

print "%d %d" % [2, 4]

but I had an error. So it seems that you *cannot* use regular lists for multiple arguments.

f  e  x


On the immutability of tuples, the most interesting difference between tuples & lists for me is not performance, but the fact that tuples can be used as dictionary keys.

This has been very useful to me, creating anonymous tuples to assert or test the uniqueness of combinations and assigning them values ( which in python can be references to just about anything )

eg:

try:
value = mydictionary[tuple(name,address,phone)]
# combination exists; do something with value
except KeyError:
# combination is new/unique ( perhaps not error-case
# but rather what you wanted in the first place

f  e  x



Add your comment below. Use an empty line between each paragraph. Paragraphs will be automatically formatted, and single carriage returns will be respected. Use <code> to begin a code block, and </code> to end a code block. Your email address will not be visible to spam harvesters or used in any way except to contact you with further questions.

Your Email Address:

Search     Home     WebLog     Consulting     Seminars     Calendar     Books     CD-ROMS     Newsletter     About     Contact     Site Design     Server Maintenance     Powered by Zope
©2007 MindView, Inc.