Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0d20cfa

Browse files
committed
Added two new questions; about globals/locals and about recursive imports.
1 parent 63d9cd7 commit 0d20cfa

1 file changed

Lines changed: 40 additions & 6 deletions

File tree

Misc/FAQ

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ Here's an overview of the questions per chapter:
175175
4.33. Q. Is there an inverse to the format operator (a la C's scanf())?
176176
4.34. Q. Can I have Tk events handled while waiting for I/O?
177177
4.35. Q. How do I write a function with output parameters (call by reference)?
178+
4.36. Q. Please explain the rules for local and global variables in Python.
179+
4.37. Q. How can I have modules that mutually import each other?
178180

179181
5. Extending Python
180182
5.1. Q. Can I create my own functions in C?
@@ -1243,12 +1245,8 @@ with native look and feel on NT, Windows 3.1 (using win32s) and on
12431245
Unix (using Tk). Source and binaries for NT and Linux are available
12441246
in <URL:ftp://ftp.python.org/pub/python/wpy>.
12451247

1246-
- Python has been mentioned on the "Futurism" subpage of the Fresco
1247-
home page <URL:http://www.faslab.com/fresco/HomePage.html>. "Pesto"
1248-
is a Python interface to the CORBA dynamic invocation interface, and
1249-
thus Fresco. A Pesto prototype is running and is currently being
1250-
packaged up for inclusion in the Fresco snapshot. See also the Pesto
1251-
web pages: <URL:http://www.faslab.com/fresco/pesto/Index.html>.
1248+
- (The Fresco port that was mentioned in earlier versions of this FAQ
1249+
no longer seems to exist. Inquire with Mark Linton.)
12521250

12531251
4.14. Q. Are there any interfaces to database packages in Python?
12541252

@@ -1696,6 +1694,42 @@ in a number of ways:
16961694

16971695
[Python' author favors solution 3 in most cases.]
16981696

1697+
4.36. Q. Please explain the rules for local and global variables in Python.
1698+
1699+
A. [Ken Manheimer] In Python, procedure variables are implicitly
1700+
global, unless they assigned anywhere within the block. In that case
1701+
they are implicitly local, and you need to explicitly declare them as
1702+
'global'.
1703+
1704+
Though a bit surprising at first, a moments consideration explains
1705+
this. On one hand, requirement of 'global' for assigned vars provides
1706+
a bar against unintended side-effects. On the other hand, if global
1707+
were required for all global references, you'd be using global all the
1708+
time. Eg, you'd have to declare as global every reference to a
1709+
builtin function, or to a component of an imported module. This
1710+
clutter would defeat the usefulness of the 'global' declaration for
1711+
identifying side-effects.
1712+
1713+
4.37. Q. How can I have modules that mutually import each other?
1714+
1715+
A. Jim Roskind recommends the following order in each module:
1716+
1717+
First: all exports (like globals, functions, and classes that don't
1718+
need imported bases classes).
1719+
1720+
Then: all import statements.
1721+
1722+
Finally: all active code (including globals that are initialized from
1723+
imported values).
1724+
1725+
Python's author doesn't like this approach much because the imports
1726+
appear in a strange place, but has to admit that it works. His
1727+
recommended strategy is to avoid all uses of "from <module> import *"
1728+
(so everything from an imported module is referenced as
1729+
<module>.<name>) and to place all code inside functions.
1730+
Initializations of global variables and class variables should use
1731+
constants or built-in functions only.
1732+
16991733

17001734
5. Extending Python
17011735
===================

0 commit comments

Comments
 (0)