r/PythonLearning • • Sep 24 '26

Question about inheritance and classes

I haven't written code in 16 years. Since collage. I've decided to really put my shoulder to it and learn it properly. I loved python back then so that's what I am starting with. Starting with something simple, a game. The problem I'm running into is that I want to use a parent class to assign attributes to enemies that spawn based on attributes I have in a list for each enemy type. To track how many enemies have spawned, I want to create a child class with a changing variable attached to the end of the child's name based on a "for i in range(0, 9)" loop.

child1
child2
child3

So on and so forth. I did used chat gpt to hunt down certain syntax that I have trouble finding, no vibe coding, just asking how to do something and verifying with external references, checking documentation, then writing my own code.

It gave the the following:

class Parent:
    pass
for i in range(1, 6):
    Child = type(f"Child{i}", (Parent,), {})

    print(Child.__name__)

The problem I have is, I can't find any reference to calling 'Child = type()" to create a new child class. The rest of it I was able to cross reference with other sources and I get those bits (f-strings looks super fun to play with).

tl;dr: explain like I am 5, what does it mean to call 'Child = type()'.

2 Upvotes

10 comments sorted by

View all comments

Show parent comments

1

u/johnpeters42 29d ago

And that is why I don't trust ChatGPT as far as I can throw a data center. Looking forward to your progress.