Hi,<div>I was wondering if there was a shorthand way to get a reference to a method object from within that method's code.</div><div><br></div><div>Take this code snippet as an example:</div><div>import re</div><div><br>
</div><div>class MyClass(object):</div><div> def find_line(self, lines):</div><div> if not hasattr(MyClass.do_work, "matcher"):</div><div> MyClass.do_work.matcher = re.compile("\d - (.+)")</div>
<div> for line in lines:</div><div> m = MyClass.do_work.matcher.match(line)</div><div> if m:</div><div> return m.groups()</div><div><br></div><div>Here, I have a method which uses a regular expression object to find matches. I want the regexp object to be tied to the function, but I don't want to have to recreate it every time the function is called (I am aware that regexp objects are cached, avoiding this problem, but I'm just using this as an example for what I want to know), so I've added it to the method object as an attribute and create it only if it doesn't exist.</div>
<div><br></div><div>However, typing out <Classname>.<MethodName>.<variablename> everytime is pretty long and susceptible to refactoring issues, so I was wondering if there was a way in Python that I am missing which allows you to reference the method that the code is in (like __module__ gives a reference to the parent module).</div>
<div><br></div><div>Paddy</div><div><br>-- <br>"Ray, when someone asks you if you're a god, you say YES!"<br>
</div>