This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Method's global scope is module containing function definition, not class.
Type: Stage: resolved
Components: Documentation Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, eric.araujo, methane, python-dev, r.david.murray, santoso.wijaya, terry.reedy
Priority: normal Keywords:

Created on 2011-03-06 23:28 by methane, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (7)
msg130201 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2011-03-06 23:28
http://docs.python.org/py3k/tutorial/classes.html#random-remarks
> Methods may reference global names in the same way as ordinary
> functions. The global scope associated with a method is the module
> containing the class definition. (The class itself is never used
> as a global scope.)

Method's function can be defined outside the module containing class
definition. And then the method's global scope is module containing
method's function definition.
msg130619 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-03-11 21:44
I’m afraid I don’t understand “Method's function”.  Could you rephrase, or give a commented code example showing the bad behavior?
msg130628 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-03-11 22:06
If you do a "def foo" in module bar.py, and have a class FooFoo in bar.py, and do:

FooFoo.myfoo = foo

and foo refers to an unbound variable, that variable will be looked up in bar.py's global name space.

If instead the 'def foo' is in a module parrot.py, and bar.py does:

from parrot import foo
FooFoo.myfoo = foo

and foo contains an unbound variable, that variable will be looked up in *parrots* global name space.

So, yes, this is a doc bug of a sort :)  It would be technically correct to change it to "The global scope associated with a method is the module containing the method's function definition", but that is not suitable for the tutorial.  In fact, the discussion of this quirk probably doesn't belong in the tutorial; unless, perhaps, in a footnote.
msg131389 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-03-19 05:11
"Methods defined within a class may reference..."
would make the tutorial correct without introducing the complication of methods defined outside a class, let alone in another module.
msg150997 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-01-10 03:11
This paragraph follows a discussion and example of the fact that methods do *not* have to be defined within a class statement.

Any objections to changing

"The global scope associated with a method is the module containing the class definition. (The class itself is never used as a global scope.)"

to 

"The global scope associated with a method is the module containing its definition. (A class is never used as a global scope.)"

?
msg151004 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2012-01-10 05:45
> Any objections to changing
>
> "The global scope associated with a method is the module containing the class definition. (The class itself is never used as a global scope.)"
>
> to
>
> "The global scope associated with a method is the module containing its definition. (A class is never used as a global scope.)"
>
> ?

+1
msg151085 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-01-11 19:57
New changeset 10a5165103f9 by Terry Jan Reedy in branch '2.7':
Minor correction. Closes #11418
http://hg.python.org/cpython/rev/10a5165103f9

New changeset 32ea3675fba2 by Terry Jan Reedy in branch '3.2':
Minor correction. #11418
http://hg.python.org/cpython/rev/32ea3675fba2
History
Date User Action Args
2022-04-11 14:57:14adminsetgithub: 55627
2012-01-11 19:57:46python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg151085

resolution: fixed
stage: resolved
2012-01-10 05:45:10methanesetmessages: + msg151004
2012-01-10 03:11:59terry.reedysetmessages: + msg150997
versions: - Python 3.1
2011-03-19 05:11:07terry.reedysetnosy: + terry.reedy
messages: + msg131389
2011-03-11 22:06:16r.david.murraysetnosy: + r.david.murray
messages: + msg130628
2011-03-11 21:44:29eric.araujosetnosy: + eric.araujo

messages: + msg130619
versions: - Python 2.6
2011-03-06 23:48:39santoso.wijayasetnosy: + santoso.wijaya
2011-03-06 23:28:34methanesetassignee: docs@python

nosy: + docs@python
components: + Documentation
versions: + Python 2.6, Python 3.1, Python 2.7, Python 3.2, Python 3.3
2011-03-06 23:28:20methanecreate