From: chad on 13 Jul 2010 23:03 Given the following code... #!/usr/bin/python class cgraph: def printme(self): print "hello\n" x = cgraph() x.printme() Does the function print() exist in the cgraph namespace or the printme() one?
From: Steven D'Aprano on 13 Jul 2010 23:37 On Tue, 13 Jul 2010 20:03:14 -0700, chad wrote: > Given the following code... > > #!/usr/bin/python > > class cgraph: > def printme(self): > print "hello\n" > > x = cgraph() > x.printme() > > > Does the function print() exist in the cgraph namespace or the printme() > one? What function print()? You're calling the print STATEMENT. It doesn't exist in any namespace, it's a Python keyword like "if", "for", "return", and similar. Note: this changes in Python 3, where print becomes a function like len(), chr(), max(), and similar. In Python 3, you would write: print("hello\n") and the function lives in the built-in namespace. BTW, print (both versions) automatically prints a newline at the end of the output, so printing "hello\n" will end up with an extra blank line. Is that what you wanted? -- Steven
From: chad on 13 Jul 2010 23:48 On Jul 13, 8:37 pm, Steven D'Aprano <steve-REMOVE- T...(a)cybersource.com.au> wrote: > On Tue, 13 Jul 2010 20:03:14 -0700, chad wrote: > > Given the following code... > > > #!/usr/bin/python > > > class cgraph: > > def printme(self): > > print "hello\n" > > > x = cgraph() > > x.printme() > > > Does the function print() exist in the cgraph namespace or the printme() > > one? > > What function print()? You're calling the print STATEMENT. It doesn't > exist in any namespace, it's a Python keyword like "if", "for", "return", > and similar. > > Note: this changes in Python 3, where print becomes a function like > len(), chr(), max(), and similar. In Python 3, you would write: > > print("hello\n") > > and the function lives in the built-in namespace. > > BTW, print (both versions) automatically prints a newline at the end of > the output, so printing "hello\n" will end up with an extra blank line. > Is that what you wanted? > I could care less about the extra blank line. I guess I was just more concerned about the namespace question.
From: Chris Rebert on 13 Jul 2010 23:52 On Tue, Jul 13, 2010 at 8:03 PM, chad <cdalten(a)gmail.com> wrote: > Given the following code... > > #!/usr/bin/python > > class cgraph: > Â Â def printme(self): > Â Â Â Â print "hello\n" > > x = cgraph() > x.printme() > > > Does the function print() exist in the cgraph namespace or the > printme() one? Neither. It exists in the built-ins namespace along with the rest of the built-in functions like len(), zip(), hex(), etc. The built-ins is the namespace of last resort; it's the last one to be consulted when trying to resolve a name in Python. You can inspect it via __builtins__ Also, in your example, print is being used as a statement (i.e. part of the language syntax), not a function; note the odd lack of parentheses when "calling" it. print was changed to a regular function in Python 3.x. Cheers, Chris -- http://blog.rebertia.com
From: Steven D'Aprano on 14 Jul 2010 00:29
On Tue, 13 Jul 2010 20:52:31 -0700, Chris Rebert wrote: > The built-ins is the > namespace of last resort; it's the last one to be consulted when trying > to resolve a name in Python. You can inspect it via __builtins__ Avoid __builtins__ as it is an implementation detail. The difference between __builtins__ and __builtin__ is one of the more confusing corners of Python, but the correct one to use is __builtin__. http://docs.python.org/library/__builtin__.html -- Steven |