Ask coding questions

← Back to all posts
Confused by subclass (Python)
Deacon_Cronin (62)

This is an example of me trying to use a subclass. In this case I'm trying to place the attribute of a subclass into a list in the upper class, and it's telling me that isn't there even though I obviously created it. I have no idea why, and any help would be greatly appreciated

Answered by ArchieMaclean (731) [earned 5 cycles]
View Answer
ArchieMaclean (731)

Here, you've created classes, but not instantiated them.
This is a bit like making a blueprint, but not making the actual model. The __init__ methods won't be called either.

To instantiate a class (create the object), you need to add () when you create it,e.g.

x = career()

This happens here, and also when you do x.job.stat. Again, add a () to instantiate then use .stat:
Or (recommended)

class career:
  class job:
    def __init__(self):
      self.stat = 5
  def __init__(self): = []
x = career()

Please mark as the answer if it solved your problem :)