0

If I inherit a class variable from a base class, and then want to use that variable as a default parameter in a method in my child class, how do I access it?

def get_records_by_domain(self, domain, search_period_days=self._X):

where _X is a variable in a parent class that is instantiated in the init of the child class?

When I try to test this function, I get the error:

NameError: name 'self' is not defined
robinhood91
  • 1,641
  • 3
  • 20
  • 36
  • 2
    go ahead and edit your question and include the two classes. – Shawn Mehan May 25 '17 at 18:55
  • Function defaults are evaluated *when the function is created*, not when called; there is no instance available at that time. Use a sentinel (like `None`) for the default and test for that in your function body: `if search_period_days is None: search_period_days = self._X`. – Martijn Pieters May 25 '17 at 18:59

0 Answers0