0

What is going on here?!

NameError: name 'Foo' is not defined

class Foo:
    def __init__(self):
        self.bar = 0.0
        self.configure(self.bar)
    def give_me_a_foo(self, var) -> Foo:
        return Foo()

Surely a class knows about itself? Do I need to to import this somehow?

ajrlewis
  • 2,968
  • 3
  • 33
  • 67
  • 2
    It's the `Foo` used to indicate the return type; at the time `give_me_a_foo` is being defined, the class `Foo` does *not* yet exist; it isn't created by the metaclass until the entire body of the `class` statement is evaluated. – chepner Jan 30 '19 at 16:26
  • @chepner Thanks -- duplicate gives the answer; you need import the following: **from __future__ import annotations**. This will become default apparently in later versions (as you'd hope!). – ajrlewis Jan 30 '19 at 16:28
  • Ah, I hadn't heard of the `annotations` future import. – chepner Jan 30 '19 at 16:32

0 Answers0