Gert Cuykens kirjoitti: > Is there a difference between > > <code> > class HelloWorld: > def index(self): > index.exposed = True > return "Hello world!" > </code> > > and > > <code> > class HelloWorld: > def index(self): > self.exposed = True > return "Hello world!" > </code>
The resident experts seemingly being absent for a while, I'll strike: Yes: the first gives a runtime error and the second is OK. I've renamed the second class to HelloWorld2 and then: >>> hw = HelloWorld() >>> hw2 = HelloWorld2() >>> hw.index() Traceback (most recent call last): File "<pyshell#144>", line 1, in <module> hw.index() File "C:\Python\Dive into Python\Py\apihelper.py", line 40, in index index.exposed = True NameError: global name 'index' is not defined >>> hw2.index() 'Hello world!' The error message shows that the Python compiler has interpreted the construction 'index.exposed' to refer to a global variable 'index' that doesn't exist at run time. The second class succesfully defines an instance attribute 'exposed' as can be seen by: >>> print hw2.exposed True HTH Jussi -- http://mail.python.org/mailman/listinfo/python-list