On Jul 26, 9:53 pm, Terry Reedy <tjre...@udel.edu> wrote: > On 7/26/2011 8:01 PM, rantingrick wrote: > > > Most new user think that printing an object to stdout is all they'll > > ever need. However when you call print -- or sys.stdout.write(object) > > -- you are only seeing a "friendly" version of the object. > > This mostly applies to strings, which *do* have 2 printed versions. It > is occasionally very important for debugging string problems.
Actually you have to be careful of objects too. Consider this: py> import Tkinter py> root = Tkinter.Tk() py> print root . py> print repr(root) <Tkinter.Tk instance at 0x02BDC4E0> py> label = Tkinter.Label(root) py> print label .46012656 py> print repr(label) <Tkinter.Label instance at 0x02BE18F0> ...as you can see, if someone overrides __str__ you can get some very confusing return values from a naked print(obj) -- which simply calls "obj.__str__()" under the covers. > > -------------------------------------------------- > > 5. id() > > -------------------------------------------------- > > http://docs.python.org/py3k/library/functions.html#id > > > > [...} > > > This is the most dangerous of the builtins, as it sometimes mislead > newbies into 'discovering' non-existent 'bugs'. The main point is that > the id of immutable objects is mostly an irrelevant implementation > detail, while the id of mutables may be critical. Lists of lists is a > particular area where id() is really useful. Yes this one can cause some major confusion to newcomers. I wonder if python should throw a warning anytime you call id() on an immutable type. py> a = "12345" py> id(a) 45313728 py> b = "12345" py> id(b) 45313728 That first call to id should have thrown a warning: Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> id(a) Warning: Comparing ids of immutable types is unreliable! and an Exception if someone tries an assertion! py> assert id(a) != id(b) Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> assert id(a) != id(b) Exception: Cannot compare immutable types! -- http://mail.python.org/mailman/listinfo/python-list