Need help with Python scoping rules

Stephen Fairchild somebody at
Wed Aug 26 02:59:45 CEST 2009

You are trying to run code in a class that does not exist yet. 

def Demo():
    def fact(n):
        if n < 2:
            return 1
            return n * fact(n - 1)
    return type("Demo", (object,), {"fact": staticmethod(fact), "_classvar":
Demo = Demo()

d = Demo()
print d._classvar    # prints 120
print d.fact(7)      # prints 5040
print Demo           # prints <class '__main__.Demo'>

Stephen Fairchild

More information about the Python-list mailing list