Global NameError Fix?

Rick Johnson rantingrickjohnson at
Fri Mar 22 18:21:08 CET 2013

On Thursday, March 21, 2013 7:24:17 PM UTC-5, Dave Angel wrote:
> On 03/21/2013 07:43 PM, maiden129 wrote:
> > Hello,
> > I'm using the version 3.2.3 of Python and I am having an
> > issue in my program and I don't know how to fix it:
> > counterLabel["text"] = str(counter)
> > NameError: global name 'counterLabel' is not defined
> [...]
> Where did you think counterLabel was defined?  It needs to
> be a dict or equivalent, in order for you to use the
> ["text"] notation on it.  If it's supposed to be an
> attribute of the CounterButton, then you should create it
> in the __init__() method, as
> self.counterLabel = {}

The decision by the Tkinter designer to allow the syntactic sugar of "__setitem__" for configuring a Tkinter widget is unfortunate. And it's not unfortunate /solely/ because of it's detrimental propagation of a well established "sugar" for "list indexing" and "dict key resolution", but compounded by the fact that misunderstands will abound from having more than one way (and preferably only one way!) of doing something.

    widget['key'] = value

...and then, when you thought the API could not get any worse, they went and created an alias for "configure":
...are three tiny little letters really enough to justify injecting multiplicity into your API? REALLY? Same foolishness goes for fetching values:
    value = widget['key']
    value = widget.cget(key)

And whilst they spared us the multiplicity of a short and long form, their choice of "cget" does not play well with "configure" or "config". 

Here's a free tutorial for all you "API designers" out there: If you want to be concise, whilst simultaneously maintain consistency between getters and setters, and you're an enemy of multiplicity, then you would want to create this API:

    value = widget.cget(key)
... where "cget" is short for "configuration_get" and "cset" is short for...oh gawd! DO I REALLY NEED TO TYPE THIS OUT FOR YOU PEOPLE!!! 


More information about the Python-list mailing list