Hi, a bug python , is it possible ? I've found a bug with the syntaxic analyser.

Justin Sheehy dworkin at ccs.neu.edu
Tue Jan 25 16:51:06 EST 2000


family cavaille <family.cavaille at libertysurf.fr> writes:

> class Bug:
>   def __init__(self):
>     print "My beautiful class don't  bug"
> 
> print "Hello"
> del Bug()
> ------------

> But I use a temporal variable, (I  replace  'del Bug()' by 'x=Bug();del
> x') It's Ok.

Well, 'del Bug()' is not valid, and 'x=Bug();del x' is.  I'll explain
that a bit more in a moment.

> For my buggous code, I have a strange comportement, because my code
> don't print Hello

If you run that code noninteractively, it will not print "Hello".
This is because the interpreter finds the syntax error before it
executes the print statement.  (or any others)

> => My Conclusion:
> del Bug() breaks the syntax analyse, -> the code is invalid -> I've not
> the print statement
> Imho del "thinks" its argument is a function (because, the () ) and not
> an instance
> 
> Have you an another interpretation ?

The 'del' statement works on bound names in the local or global
namespace, not on objects.

That is, given your class definition, you could do the following:

>>> x = Bug()
My beautiful class don't  bug
>>> x      
<__main__.Bug instance at 80dff10>
>>> del x
>>> x
Traceback (innermost last):
  File "<stdin>", line 1, in ?
NameError: x

Note that the main effect of the del statement here is to make it such 
that the name 'x' no longer refers to anything.

Given this behavior, 'del Bug()' would be nonsense, as it would be
trying to make the function call 'Bug()' refer to nothing.  Since
function calls (different from function objects) are not names that
point to anything, this is impossible.

It would be valid to write 'del Bug', though that might not be what
you wanted.  It would make the name 'Bug' no longer refer to that
class definition,  and thus make it impossible for you to make any
more instances of that class.

I hope that this has made things clearer for you.

-Justin

 




More information about the Python-list mailing list