class Outer(object):
class InnerBase(object): _var = {'foo', 'bar'}
class Derived(InnerBase):
_var = _var | {'baz'} # NameError: name '_var' is not defined
_var = InnerBase._var | {'baz'} # name 'InnerBase' is not defined
_var = Outer.InnerBase._var | {'baz'} # free variable 'Outer'
# referenced before assignment in enclosing scope
Moving _var in Outer
does not help - moving it in module scope would work but defeats the purpose of having classes. So how to go about that ?
EDIT: coming from Java so the scoping rules of classes are a head scratcher for me - a briefing would be appreciated. This works btw:
class Derived(InnerBase): pass
Derived._var = InnerBase._var | {'baz'}
but it's not the pinnacle of elegance.
Related: Nested classes' scope? - but here we specifically want to access our parent class (rather than the Outer type)
EDIT2: What I am actually after is a _var = __class__._var
-like syntax (or hack), or an explanation as to why it's not there
Derived
) have actually _var already ? (If only_var = __class__._var
would compile...) – Liberticide