how do you get the name of a dictionary?

Robert Kern robert.kern at gmail.com
Wed Aug 23 03:44:28 EDT 2006


Steven D'Aprano wrote:
> On Wed, 23 Aug 2006 08:56:54 +0200, Fredrik Lundh wrote:
> 
>> Steven D'Aprano wrote:
>>
>>> But an upside is that it would enable more useful error messages, at least
>>> sometimes. Here's some trivial pseudo-code:
>>>
>>> def foo(a):
>>>     assert len(a) > 10, "%s is too short" % a.__name__
>>>
>>> y = "hello"
>>> foo(y)
>>>
>>> would display "AssertionError: y is too short".
>> why not "a is too short" ?
>>
>> or for that matter, "x is to short" ?
> 
> These are all valid responses too. But consider that when you get an
> exception that says "a is too short", you often have to mentally change
> gears and think about where a came from and what it is called in the
> enclosing scope. After all, if the value of a is invalid, and the value of
> a is set in the enclosing scope, it makes sense to refer to "the object
> known locally as a" by the name it was known as when it was set.

That's what tracebacks are for. You don't have to mentally change gears; you 
just look.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list