Need help with Python scoping rules

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Thu Aug 27 04:07:46 EDT 2009


Ulrich Eckhardt a écrit :
(snip)

> Now, what actually causes problems is that 'fact_rec' is not universally
> accessible until class 'Demo' is fully defined, but you need this in order
> to fully define it. Here comes a drawback of the dynamic nature of Python,
> that the declaration phase (compiling) is not separate from the execution.

It is. You can manually compile a module, remove the source .py and 
still import (or execute) the compiled .pyc.

The "problem" here is not with compilation, but with the fact that 
'class' (and 'def') statements are *executable* statements. IOW, the 
class statement is executed when encountered, that is, usually (most 
class statements being top-level statements), when the module is first 
loaded.


> I fully agree that this case is rather vexing to my (and obviously your)
> biased brain. I wouldn't call this a bug before fully understanding why
> e.g. you can not access a class before its definition is finished.

Nor why it's a GoodThing(tm) for quite a lot of use case - while the 
OP's problem is in the splitting-hairs category - only a problem if yoçu 
insist in imposing your views on the language instead of learning how to 
use it.

> Suggestion: someone mentioned that you should make the function a normal
> function. 
(snip)

Someone (Diez Roggisch IIRC) also mentionned a simple solution that 
mostly fulfills the OP's desires: use a nested function.



More information about the Python-list mailing list