<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im"><br></div>
But <a href="http://docs.python.org/tutorial/classes.html" target="_blank">http://docs.python.org/tutorial/classes.html</a> says, in Section 9.3 "A First Look at Classes":<br>
<br>
When a class definition is entered, a new namespace is created,<br>
and used as the local scope — thus, all assignments to local variables<br>
go into this new namespace. In particular, function definitions bind<br>
the name of the new function here.<br>
<br></blockquote><div>[snip] </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><br>
(BTW, Diez, your toy example is another demonstration that there *is* a class-scope-lookup: the "def" statement binds the name "fact" in the class scope, and the assignment statement looks up the name "fact" in that scope.)<br>

<font color="#888888">
</font></blockquote><div><br></div><div>This sounds like a fundamental confusion -- a namespace is not equivalent to a scope, really, I think.</div><div><br></div><div>The def statement creates a function object, and assigns it to the given name, in its /local scope/ which is the class's namespace -- but that is not a new scope. Its just, during class creation, the local scope.</div>

<div><br></div><div>When you enter a class definition, a new namespace is created, and that is used as the local scope during the class's definition. Under "class Foo", the local scope is this class's namespace-- and the global scope is the module's namespace. Any lookup operation checks first in the local scope, then in the global scope... </div>

<div><br></div><div>When the "def fact(...)" is executed (note that class bodies are executed on load, function bodies aren't), a new namespace is created as well-- the function's namespace-- and its used as the local scope. During execution of the code, it looks up in the local scope first-- and then it looks up in the global scope if it doesn't find anything. There /is/ no class-lookup-scope... but there IS a class namespace.</div>

<div><br></div><div>There's just those two scopes: but the namespace bound to those scopes changes as you enter different parts of the code. </div><div><br></div><div>For a long time that's all there was, just the local and global scope: to access any other namespace required you to explicitly address it. The class namespace remains accessible only via explicit addressing, but PEP 227 in Python 2.1/2.2 introduced static nested scopes. But that applies only to enclosing functions: embedding one function into another.</div>

<div><br></div><div>You can only call recursive functions if the function is able to refer to itself according to the same lookup rules: is the function's name in the local scope? No it's not... is it in the global scope? No? Then it can't call itself recursively... well, unless its a method, and it addresses itself specifically-- with "self.".</div>

<div><br></div><div>The OP's probably is that during the execution of the class body, the class doesn't exist... so the 'def fact' -can't- use Classname.fact to address itself explicitly, and within fact the locals don't contain a reference to the function itself, and its globals don't either. You just can't do that. The right way, IMHO, is to move 'fact' up and out of the class into a _fact global variable. Alternatively, you can use a metaclass.</div>

<div><br></div><div>--S</div><div><br></div><div><br></div></div>