[Tutor] Question about dictionary method get()

Alan Gauld alan.gauld at blueyonder.co.uk
Mon Jun 28 18:37:13 EDT 2004


> Uh, oh. I just now tried      print D.get("v")     and did get
"None".
>   Then why not with the plain    D.get("v")  ?  Maybe I've found the
> source of my confusion?

Python has two methods __str__ and __repr__ that convert objects
to strings.

When you evaluate an object

>>> x
42

You are calling __repr__ on x

When you print an object

>>> print x
42

You are calling the __str__ method of x.

Often str and repr produce the same result but not always.
None is an example of a difference.

Notice too the difference when dealing with strings....

Alan G.




More information about the Tutor mailing list