python namespace question

Steven D'Aprano steve-REMOVE-THIS at cybersource.com.au
Tue Jul 13 23:37:03 EDT 2010


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
 



More information about the Python-list mailing list