Assume I have some class c
, and I would like to create a function f
which gets a parameter, which defaults to self.x
if not given. i.e.
class c:
def __init__(self, x):
self.x = x
def f(self, n = self.x):
...
However, this doesn't seem to work (name 'self' is not defined
).
Is there a solution for this problem or is it not possible to use a member as a default for the function?
A simple solution would be something like:
def f(self, n = None):
if (n is None):
n = self.x
But I was wondering if it's possible to avoid it.