pygame - importing GL - very bad...

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jan 2 02:39:31 EST 2013


On Wed, 02 Jan 2013 00:49:36 +0100, someone wrote:

> On 01/01/2013 12:49 PM, Steven D'Aprano wrote:
>  > On Tue, 01 Jan 2013 12:00:32 +0100, someone wrote:
>  >
>  >> See this code (understand why I commented out first line):
>  >>
>  >> # from OpenGL.GL import *
>  > [...]
>  >> The reason why I commented out the first line is that I use "pylint"
>  >> and it reports: "[W] Redefining built-in 'format'" for this line.
>  >>
>  >> From: http://www.logilab.org/card/pylintfeatures tell: W0621:
>  >> Redefining name %r from outer scope (line %s) Used when a variable's
>  >> name hide a name defined in the outer scope.
>  >>
>  >> I don't like to redefine already defined names so therefore I had to
>  >> outcomment first line and then keep on adding stuff until I could
>  >> run my program... But this SUCKS! I can see that pygame hasn't been
>  >> updated for a long while - not many users use it? I'm not very happy
>  >> about this...
>  >
>  > from pygame import *
>  > del format
> 
> Are you sure about this? Because I'm not (OTOH I'm maybe not as
> experienced in python as some of you)...

In the general case of deleting global names that shadow builtin names, 
yes I am.

In the specific case of importing * from pygame, no. I trusted you that 
pygame exports format. Unfortunately, it seems that you were fooled by an 
invalid warning from pylint, so we were both mistaken.


> Ipython log:
> 
> --------
> In [6]: test=format(43)
> In [7]: type(test)
> Out[7]: str
> In [8]: from pygame import *
> In [9]: test=format(43)
> In [10]: type(test)
> Out[10]: str
> In [11]: del format
> ------------------------------------------------------------------------
> NameError                              Traceback (most recent call last)
> <ipython-input-11-028e6ffb84a8> in <module>() 
> ----> 1 del format
> 
> NameError: name 'format' is not defined 
> --------
> 
> What does this mean? Why does it say 'format" cannot be deleted after I
> did the wildcard import ?

It means that there is no "format" in the current scope, which implies 
that pygame no longer has a "format" which can be imported.

You don't need an import to shadow built-ins. See for example:

py> format
<built-in function format>
py> format = "NOBODY expects the Spanish Inquisition!"
py> format  # this shadows the built-in "format"
'NOBODY expects the Spanish Inquisition!'
py> del format  # get rid of the Spanish Inquisition
py> format 
<built-in function format>
py> del format
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'format' is not defined


When a name is not discovered in the current scope, the builtin scope is 
checked before Python gives up and reports a NameError. But del only 
works on the current scope, to stop you from accidentally deleting the 
wrong object.


>  > pylint may still complain, but you can ignore it. By deleting the
>  > name "format", that will unshadow the builtin format.
> 
> Are you sure?

Since it turns out that pylint was actually wrong to complain, no format 
was actually imported, then yes you can safely ignore it :-)



-- 
Steven



More information about the Python-list mailing list