python namespace question

C

chad

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?
 
S

Steven D'Aprano

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?
 
C

chad

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.
 
D

Dennis Lee Bieber

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... There is NO "function print()" in your example as it
isn't a function but a language keyword (just like "def", "if", "class")
starting a statement.

Print IS a function in Python 3.x, but then it needs () around the
argument.
 
C

Chris Rebert

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
 
S

Steven D'Aprano

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
 
A

alex23

chad said:
I could care less about the extra blank line. I guess I was just more
concerned about the namespace question.

Which is why Steven spent far more time answering that question than
commenting on newline handling.

Now say 'thank you'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top