0

I want to create an object car using inner classes, this object car have attributes like number of seats, color, year, engine. The last attribute engine capacity will have another attributes like number of valves, type of fuel, kms by lts.

so I am creating a class car first and then engine class:

class car:
     def __init__(self, color, year, engine):
        self.color = color
        self.year = year
        self.engine = engine

class engine:
    def __init__(lts, kms_by_lts, fuel_type, valves ):
        self.lts = lts
        self.fuel = fuel
        self.valves = valves
        self.kms_by_lts = kms_by_lts

>> my_car = car('blue','2010','v6')
>>> my_car.engine
'v6'

I want to access a class inside another class like as follow:

>>> my_car.v6.lts = 4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: car instance has no attribute 'v6'

Could help me with this issue.

Regards

I recreate the object as follow:

class car:
     def __init__(self, color, year, engine):
        self.color = color
        self.year = year
        self.engine = engine()

class engine:
    def __init__(lts, kms_by_lts, fuel_type, valves ):
        self.lts = lts
        self.fuel = fuel
        self.valves = valves
        self.kms_by_lts = kms_by_lts

Having the following error

>>> my_car = car('blue','2010','v6')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in __init__
TypeError: 'str' object is not callable
9
  • The attribute engine has value v6. Why do you want to refer to the attribute by a different name? Commented Jan 28, 2018 at 6:59
  • >>> my_car.engine.lts = 4 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'str' object has no attribute 'lts' Commented Jan 28, 2018 at 7:07
  • That’s the vale you gave it. You need to create an engine object if you want it to have attribute ‘lts’ Commented Jan 28, 2018 at 7:09
  • 1
    But you’re setting engine to be a string not an engine object Commented Jan 28, 2018 at 7:12
  • 1
    self.engine = engine() does not help you here. In your case the engine that is being used is in fact the string "v6" that you passed. Read the link I posted in my previous comment. Commented Jan 28, 2018 at 7:29

1 Answer 1

2

You need to construct an engine first, then a car:

my_engine = engine(8.4, 5.5, 'petrol', 20)
my_car = car('blue', '2010', my_engine)

Now you can access my_car.engine.lts, for example.

Sign up to request clarification or add additional context in comments.

3 Comments

Would be good to note that for this to work, they should not be using the latest version of their code, but rather the original version. The latest version is doing self.engine = engine()
@idjaw: Nice note, indeed I wrote this against the first chunk of code in the question, not the chunk lower down.
Thanks idjaw, its working now, and thanks again for the suggestion. Thanks Peter for your cooperation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.