2

I'm reading the python tutorial. The 3rd paragraph confuses me a bit.

"Clients should use data attributes with care — clients may mess up invariants maintained by the methods by stamping on their data attributes."

What exactly do they mean by invariants? Do they mean data attributes that certain methods rely on? (e.g a method that returns a certain data member; i.e. a getter method)

Christophe De Troyer
  • 2,660
  • 2
  • 26
  • 41
  • Take a look at http://stackoverflow.com/a/112088/367273 as it might clear things up. – NPE Jan 02 '14 at 21:23
  • Oh exactly! :) I had found some questions, but not that one! So in anology with the BTree, overwriting the left node of a tree could damage the invariance in the sense that the tree might be bigger. I get it, Thanks – Christophe De Troyer Jan 02 '14 at 21:25

1 Answers1

1

I believe what they want to say there is that you should be careful when accessing/mutating an object’s properties. (Note that I call them “properties”, not “data attributes”)

This is because an object may put something in a property that might be necessary to maintain an object’s state, i.e. an invariant which you would normally not be given means means to mess with.

But unlike other programming languages, Python does not have any protection for object members. There are no private instance variables or methods, so anyone can change an object in the way they want to, possibly completely destroying its functionality.

So the tutorial suggests you to avoid storing important things in properties—but to be honest, there is not really a much better way, and if someone would want to mess with it, they just could. For example you can swap out whole methods at run time, replacing them with a completely different logic.

So trying to protect yourself too much is not really worth it. Instead, I would suggest you to just document everything in a clear way. E.g. point out which properties are free to touch by users, and which are for internal use only.


† There are some means and conventions, e.g. one leading underscore for internal/protected members, and two leading underscores for private members, but those will not technically prevent you from accessing it, so it’s not a protection.

poke
  • 307,619
  • 61
  • 472
  • 533
  • "the tutorial suggests you to avoid storing important things in properties" -- I don't think it does suggest that. It says *clients* (which it defines as: ordinary users of the object) should use data attributes with care. That implies nothing about how the class itself should store important things, just that outsiders shouldn't poke around unless they know what they're doing. For that matter, "use with care" is a pretty weak warning in programming terms. Code written with no care very rarely works, no matter which language features it uses ;-) – Steve Jessop Jan 02 '14 at 22:47
  • For me, if a library for example offers some types to be used, then users of that library are its clients. This analogy works for big libraries with lots of functionality, and with single small types. If I use something which is not mine (although it also applies to my stuff), then I should use it the way it was intended to be used (i.e. I’m an outside and I shouldn’t *poke* around). And that’s exactly what should be documented. – poke Jan 02 '14 at 23:58