[Tutor] Variable scope for class?

Kent Johnson kent37 at tds.net
Fri Aug 31 00:33:20 CEST 2007

Orest Kozyar wrote:
> I'm trying to follow the example listed in the wiki at
> http://www.sqlalchemy.org/trac/wiki/UsageRecipes/UniqueObject regarding the
> use of a metaclass.  
> What I don't understand is how the metaclass (EntitySingleton) has access to
> the variable ctx which is instantinated outside the scope of the class
> definition.  Could anyone point me in the right direction please?

This is just basic name lookup rules. Names are looked up, in order,
- in the scope of the current function
- in the scope of any lexically enclosing functions
- in the scope of the module containing the function (the 'global' scope)
- in the built-in scope.

ctx is defined at module level so the third step of the name lookup 
finds it.

Not finding any great references but here are a few that might help:


More information about the Tutor mailing list