1

For example:

class Car: pass jeep = Car() jeep.color = 'blue' 

This is perfectly legal and runs fine. I understand that python is pretty dynamic but doesn't this violate some principle of oop?

9
  • 5
    Which principle of oop are you referring to? Commented Dec 27, 2011 at 21:11
  • It could be argued that this violates the principle of data hiding, but then Python doesn't enforce that at all. Commented Dec 27, 2011 at 21:12
  • New attributes belong to the instance, not class Commented Dec 27, 2011 at 21:13
  • Well I thought one of the main features of oop was protection. Attributes belonging to an instance and not the class makes sense but then it doesn't. What's the point of having a class if you have instances adding any attribute they want Willy Billy? Commented Dec 27, 2011 at 21:23
  • "Well I thought one of the main features of oop was protection"? Can you please update the question to define what you think you mean by "protection". Can you provide a link or a quote or a reference to where you've seen this as an essential feature of OO programming. Commented Dec 27, 2011 at 21:25

1 Answer 1

2

http://docs.python.org/tutorial/classes.html#odds-and-ends

To dynamically set up the fields and value when they can only be known at run time.

Python does not prevent you from dynamically creating instance properties, though you can decide not to do so if you have no very good reason to do so. Python is quite a powerful language, though care must be excercise to avoid abusing this sort of power to do crazy things with it's flexibility.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.