[Tutor] declare a variable inside a class

richard kappler richkappler at gmail.com
Thu Feb 11 15:28:21 EST 2016


Trying to optimize the modular input we wrote for Splunk, the basic
structure (dictated by Splunk) is the entire script is a class with
numerous methods within. The modular input reads a tcp stream, starts a new
thread for each source, reads the messages from that source into a buffer,
checks for stx and etx, formats it using an xslt and sends the result out
to splunk.

The method that actually formats the messages opens the xslt file, then
uses it to format the message. Clever boy that I am, I figured it would be
better to open the xslt file once, rather than once per message, so I moved
the line

xslt = ('path/to/xslt.file')

out of the method it was in and up to the top level of the class, to wit:

class MyClass():

    xslt = ('path/to/xslt.file')

   def a_bunch-of-methods():

and so on.

Obviously this didn't work, when the formatting method was called, it
through an exception that the variable xslt wasn't defined. This is my
first not-a-toy try at OOP, I could use a clue.

regards, Richard

-- 

*Java is like Alzheimers; it starts slow and eventually, it takes away all
of your memory.*


More information about the Tutor mailing list