Python hierarchy in classes

Hello,

After searching through subclasses, metaclasses and nested classes, I am more confused than before.

So I wanted to ask you:
How would you structure an object that have objects related to it?

For example:
A Car has got single parts. Let’s suppose that a part can only belong to one car.

So I maybe create an Instance of Car with the Name “FirstCar”. Then I could create a part of “FirstCar” like “FirstWheel”.

This is working. But how could I access variables from the parent? I have read, that nested loops are a messy approach anyway. How would you solve this?

Best,
Chris

class car:
list = []

def __init__(self, name, type):
    self.name = name
    self.type = type
    self.list.append(name)

class part:
    list = []
    def __init__(self, name):
        self.name = name
        self.list.append(name)
    
    def Where_do_you_belong_to(self):
        print("I belong to a car of the type " + car.type)

FirstCar = car(“FirstCar”, “Truck”)
FirstCar.FirstWheel = car.part(“Firstwheel”)

FirstCar.FirstWheel.Where_do_you_belong_to()

It really depends on how you want to use your class structure. If you are just trying to connect KX_GameObjects together in a parent/child relationship then you don’t even need custom classes.

If you want to model a car where you can change the wheels out to change the car’s performance then building something like this starts to make a bit more sense.

But how you design your classes all comes back to the operations you need them to perform and/or the types of calculations they need to do. So I imagine a system where you will have a car (that has 4 wheels) and the traction of the car depends on the type of wheels on it.


# Defines a car object
class Car(object):

  def __init__(self):

    # Tracks wheels on the car.
    self.wheels = None

  def getTraction(self):
    return self.wheels.getTraction()

class Wheels(object):
  
  def __init__(self, traction):
    self.traction = traction

  def getTraction(self):

    # Note, in this case you don't need a function.  But if the traction was different depending on
    # your surface then you might need to compute.  For example, racing slicks are great for
    # drag racing but have terrible traction in the rain.
    return self.traction

Then you can build a car like this:



car = Car()
car.wheels = Wheels(1.0)  # Friction rating of 1.0, whatever that might mean.

print(car.getTraction())

car.wheels = Wheels(2.0)  # Got an upgrade!
print(car.getTraction())

But what if you want to track each wheel separately? You might have a complicated simulator where you track wear on each wheel separately. Then your Car class needs to have a separate Wheel that has a traction and health rating.

You probably want to keep the system fairly simple where a Car class holds Wheels, Engine, Exhaust, Suspension classes that model the specific parts of your car.

If you need to get super fancy where you have a collection of ‘parts’ and you can create a vehicle by combining them then you might need something like a Parts class where you can inherit Wheels, Engine, Exhaust, etc classes so they are all parts. Then your Car class would be a collection of Parts and your Car would need some fancy logic to validate that it had a valid combination of Parts assigned to it.

So, what kind of car game are you building? Because what you want to do determines how you should go about doing it.

Hi Kastoria,
thank you very much for your precise answer and the examples!
That makes it more clear to me.
Best,
Chris

the snippet was very clear and understandable…thank you for sharing your valuable info.