Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Instant Objects Method Arguments

Why setattr

I understand you can use setattr to accept unknown attributes but can someone give me real life examples of this in practice? I need the why behind the why! What kind of projects would benefit from this? A few ideas would help out greatly, thank you!

1 Answer

Nemanja Savkic
Nemanja Savkic
17,418 Points

Let's say you want to create a class Car. You can add parameters like self, color, numb_of_seats, numb_of_doors and these need to be filled when creating an instance of Car. Later on, someone wants to create an instance of Car which has 3 engines (numb_of_engine = 3) and 8 wheels (number_of_wheels = 8) (not a car anymore). These parameters weren't provided before so that's how we can add them later, without changing the code. And there can be an unknown number of these parameters, with all kinds of names.

I'm also new to this and that's how I understood it. Hope it helps