This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| script:walkthrough:scopes [2019-11-25 06:34] – [Importing modules] skyjake | script:walkthrough:scopes [2019-11-25 08:55] (current) – [Importing modules] skyjake | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | <- Functions ^ ^ Miscellaneous -> | ||
| + | |||
| + | ====== Scopes ====== | ||
| + | |||
| + | The following scopes (namespaces) are defined: | ||
| + | |||
| + | * Each module has its own global scope. | ||
| + | * Each function has its own local scope. | ||
| + | * In a class definition, the scope is the [[records# | ||
| + | |||
| + | ===== Global assignment ===== | ||
| + | |||
| + | The global assignment '': | ||
| + | |||
| + | a = 1 | ||
| + | def globalAssign() | ||
| + | a := 2 | ||
| + | end | ||
| + | globalAssign() | ||
| + | print a | ||
| + | |||
| + | Output: | ||
| + | |||
| + | 2 | ||
| + | |||
| + | ===== Local namespace ===== | ||
| + | |||
| + | Built-in function '' | ||
| + | |||
| + | # script " | ||
| + | a = ' | ||
| + | print locals() | ||
| + | |||
| + | Output: | ||
| + | |||
| + | __file__:/ | ||
| + | a: hello | ||
| + | b: world | ||
| + | |||
| + | ===== Importing modules ===== | ||
| + | |||
| + | The '' | ||
| + | |||
| + | import App | ||
| + | |||
| + | When a module is first imported, its global namespace is initialized by executing all statements in the module' | ||
| + | |||
| + | The import statement can also be used to make a copy instead of regular importing. | ||
| + | |||
| + | import record other | ||
| + | |||
| + | This imports //other// (from a file called **other.ds** somewhere on the import path), makes an owned copy of the record, and assigns it to a local variable named //other//. It is equivalent to the following: | ||
| + | |||
| + | import other | ||
| + | other = Record(other) | ||