My apologies if this is a trivial question. I am trying to pass a subclass variable as an argument in the parent class __init__()
but get an 'undefined name' warning.
For example: The subclass (JumpingJacks
) variable PATH
is considered an 'undefined name' when I try to use it in the parent class (Exercise
) instantiation.
class JumpingJacks(Exercise):
"""Creates JumpingJack instance, an Exercise with default attributes
common to the Exercise (parent) and unique to JumpingJacks:
Exercise(name, reps, per_side=False, path=None)
"""
PATH = "Jumping Jacks.PNG"
def __init__(self, reps, name="Jumping Jacks"):
Exercise.__init__(self,
name, reps,
per_side=False,
path=PATH)
I suspect the answer relates to class inheritance, but because PATH
is local during the parent class __init__()
, I don't understand why PATH
is undefined. I have been unable to find a succinct explanation. What part of this am I misunderstanding or doing incorrectly?
I realize I could hard-code the path into the parent class __init__()
like this:
path="Jumping Jacks.PNG"
Rather than:
path=PATH
But I think its better practice to use the class variable instead if all instances share the common variable. Correct me if this way of thinking is incorrect.
How do I call the subclass variable in the parent class __init__()
?