I'm writing some code, in which a class defines a few nested classes (to keep things neater and grouped by purpose). I want the nested classes to use the attribute of the enclosing class (see below). This attribute would provide a default value that is set on top of the enclosing class and not a 'magic number'.
class Progress_indicator:
#use this attribute to provide a default value
number_of_dots=5
class Dots:
#the attribute above provides a default value here:
def __init__(self, number = Progress_indicator.number_of_dots):
self._current=0
self._number_of_dots=number
Now, when the above code is run I get:
NameError: name 'Progress_indicator' is not defined
from the line containing __init__.
I understand that this is due to python defining a namespace as it sees a class declaration but actually assigning that namespace to a name (Progress_indicator in this case) after the full class declaration is processed (i.e. the indented block of code is left). Redefining __init__ to use self.number_of_dots instead of Progress_indicator.number_of_dots generates the same error (as there is no inheritance here). All references I tried (books, and lots of web) suggest that the above should work. But it doesn't. Is there a neat way of accessing the attribute defined in the enclosing class to provide a default value for a function parameter as above?
I'm using python3.2.