[Tutor] declare a variable inside a class

Peter Otten __peter__ at web.de
Thu Feb 11 16:20:29 EST 2016


richard kappler wrote:

> Thanks for the reply Martin, and in this instance I cannot post the actual
> code (company rules). What I can do is say that with the xslt variable
> defined within the formatter method, everything works, but when I pull it
> out and put it in the upper level of the class, it gives me a traceback
> that says the global variable xslt is not defined. Does that help?

You need to qualify the class attribute with self to access it from within a 
method:

>>> class MyClass:
...     xslt = "/path/to/file"
...     def some_method(self):
...         print(self.xslt)
... 
>>> m = MyClass()
>>> m.some_method()
/path/to/file




More information about the Tutor mailing list