As the title indicates, I am trying to pass an attribute of my class as a parameter for a function of that same class. In the example below, the functionality of print_top_n()
would be to print self.topn
by default, but the function could also be called with a different value if need be. Is this a Python (or general programming) foul or is there a way to do this?
>>> class Example():
def __init__(self, topn=5):
self.topn = topn
def print_top_n(self, n=self.topn):
print n
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
class Example():
File "<pyshell#7>", line 4, in Example
def print_top_n(self, n=self.topn):
NameError: name 'self' is not defined