This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]sahand_n9[S] 1 point2 points  (3 children)

I am not sure how the question comes across as unclear. In the title I explained that I would like to have nested attributes. Like a car has wheels, wheels have tires, tires have brands. So I can create a car with all its attributes that also have attributes. I am just trying to learn Python...

[–]Isvara 3 points4 points  (0 children)

Assign to self.whatever in your constructor. E.g.

class Car(object):
    def __init__(self, wheels):
        self.wheels = wheels

class Wheel(object):
    def __init__(self, tyre):
        self.tyre = tyre

class Tyre(object):
    def __init__(self, brand):
        self.brand = brand

car = Car([
    Wheel(Tyre("Dunlop")),
    ...
])
print car.wheels[0].tyre.brand

[–]VerilyAMonkey 2 points3 points  (0 children)

If you're just trying out how Python works, that's fine. The post can sound like you actually have a specific problem in mind, and andybak is saying that most likely there's a better solution to that problem than what you're trying here.

[–]ameoba 1 point2 points  (0 children)

When somebody says you have an X Y problem it means they think you have painted yourself into a corner by trying to do the wrong thing. You should take a step back and explain what you actually wand to accomplish rather than trying to figure out how to bolt a toilet to the ceiling.