[New-bugs-announce] [issue22574] super() and del in the same method leads to UnboundLocalError

Pierre-Antoine BRAMERET report at bugs.python.org
Tue Oct 7 11:48:15 CEST 2014


New submission from Pierre-Antoine BRAMERET:

Hi,

With the following code:

class Base(object): pass

class Foo(Base):
  def __init__(self):
    super(Foo,self).__init__()
    if False:
      del Foo


I expect that Foo() would give me a Foo instance. Instead, it raises the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __init__
    super(Foo,self).__init__()
UnboundLocalError: local variable 'Foo' referenced before assignment



I first suspected the "del Foo" statement is executed before the function is executed (while parsing or something), because the error tells Foo does not exists.

Howver, the problem is deeper than that: with the following modified code:

class Foo(Base):
  def __init__(self):
    assert 'Foo' in globals()
    assert Foo
    super(Foo,self).__init__()
    if False:
      del Foo

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in __init__
    super(Foo,self).__init__()
UnboundLocalError: local variable 'Foo' referenced before assignment



So: Foo IS in globals(), but cannot be accessed through Foo in the class because of the presence of 'del Foo' in the never reached part of the method.

----------
messages: 228757
nosy: Miaou
priority: normal
severity: normal
status: open
title: super() and del in the same method leads to UnboundLocalError
versions: Python 2.7, Python 3.2, Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22574>
_______________________________________


More information about the New-bugs-announce mailing list