Scope of definitions

This section discusses the scope (visibility) of variables. Variables, functions and other definitions can only be accessed in expressions and other contexts when they are visible. If a definition is not visible at some location, its name has no special meaning at that location and acts as if the definition did not exist at all.

Sometimes another definition with the same name shadows a visible definition. There are different ways of accessing global and member definitions, and some of these can often be used to access shadowed definitions.

Global definitions

All global definitions are visible in all the files of the module that defines them. Public global definitions are also visible in the files that import the module that defines them.

Class members

Class members can be accessed in two ways: through the self object or through another object. Access through the self object is possible by accessing the name of a member directly in the body of a class without the member access operator "." or by using the member access operator with the self keyword as the object.

All members defined in a class, public and private, and all public members defined in any superclass can be accessed through the self keyword. If members are accessed through any other means, only public members defined in the class or inherited from a superclass can be accessed.

If a name is overridden in a subclass, i.e. there is another public definition with the same name as a public definition in a superclass, the definition in the subclass will take precedence over the definition in the superclass.

Local variables

Local variables declared using the var keyword are visible only in the block that defines them, and only in the statements succeeding the variable definition. Method and function formal arguments are visible in the entire body of the method or the function. A local variable is also visible within any anonymous function expression (both in the argument list and the body of the anonymous function) that is within the visibility range of the local variable.

The item variable in a for loop and the exception variable in a try-except statement are visible only in the block that follows the variable declaration.

Precedence of names

If an identifier may refer to multiple visible definitions, these ambiguities will be handled in the following manner:

  1. Local variable definitions take precedence over all other definitions.
  2. Member definitions take precedence over global definitions.
  3. Global names defined in the module that contains the reference take precedence over names defined in other modules.
  4. Fully qualified names take precedence over not fully qualified names.

If the name cannot be disambiguated with these rules, a compile error will be generated.

Name clashes

It is an error to define multiple definitions with the same name in a single scope. In particular: