Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Classes - Using Instances AS Attributes

I'm currently learning/working with classes in Python 3.10.2 as of writing this. What I am trying to achieve is to create a class instance which is an attribute within another class.

Here is some code I've been working on to help demonstrate my point.

class Vehicle():
    """To model a basic vehicle"""
    def __init__(self, name, max_speed, millage):
        """Initiate class variables"""
        self.name = name
        self.max_speed = max_speed
        self.millage = millage
        self.tyre = Tyre()

    def vehicle_details(self):
        """display vehicle details"""
        return f"\nName: {self.name.title()}, Max speed: {self.max_speed}MPH, Millage: {self.millage} "

class Tyre():
    """A class specific for vehicle tyres"""
    def __init__(self, size=16, pressure=36):
        self.size = size
        self.pressure = pressure

    def tyre_details(self):
        """Print tyre details."""
        print(f'Tyre size: {self.size} inches.')
        print(f"Tyre pressure {self.pressure} PSI.")

In the Vehicle class, I add Tyre() as an attribute.

Now this code DOES work and I can call the Tyre methods through my Vehicle instances, but only when I allocate pre-determined values to size and pressure within the Tyre class.

Is there either:

  1. A way I can achieve this without having to allocate the pre-determined values within the Tyre class?

    This is the Traceback I receive if I do not allocate the pre-determined values:

    TypeError: __init__() missing 2 required positional arguments: 'size' and 'pressure'
    
  2. An easy way for me to overwrite the pre-determined values when calling this class and its methods if I am unable to remove the pre-determined values?

like image 712
ScottMButler Avatar asked Mar 08 '26 11:03

ScottMButler


1 Answers

  1. A way I can achieve this without having to allocate the pre-determined values within the Tyre class?

A possible solution might be to pass the arguments for Tyre when initializing the Vehicle:

class Vehicle:

    def __init__(self, name, max_speed, millage, tyre_size=16, tyre_pressure=36):
        """Initiate class variables"""
        self.name = name
        self.max_speed = max_speed
        self.millage = millage
        self.tyre = Tyre(size=tyre_size, pressure=tyre_pressure)
  1. An easy way for me to overwrite the pre-determined values when calling this class and its methods if I am unable to remove the pre-determined values?

You can simply not use any pre-determined values, requiring them to being explicitly provided:

class Tyre:

    def __init__(self, size, pressure):
        # ...

If no value is passed an exception will be raised. You should rely on the pre-determined values only if you really want to. Instead of always relying on a specific set of pre-determined values.

In general,I would suggest only using pre-determined values when you rarely need a different value from the default. For example, if you have a User class probably you'll always use a different username. There's no obvious default value. In the other hand, when you have a Vehicle class, in most of the cases it will have 4 doors, but sometimes might be different (maybe 5), so you can 4 as default and change it when you need.

like image 152
aaossa Avatar answered Mar 11 '26 01:03

aaossa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!