Safe to use property names inside class methods?
<p>Given a class with a read-only property, one can modify it quite easily:</p>

<pre><code>cls.prop_name = property(cls.prop_name.fget, my_setter)
</code></pre>

<p>However, one might <code>del cls.prop_name</code> or change it as I did. This is considered acceptable by PEP 8, which states <code>prop_name</code> is a public attribute and is therefore usable by any user of the class however they like, similar to just modifying the non-public attribute I'm hiding behind the property directly and bypassing any checks I might have created, such as restricting an RGB component's value to the range 0..255.</p>

<p>This would be a bad idea according to PEP 8 if the property name began with an underscore, but it doesn't, making it OK to delete or modify the property in some way. This is the exact sort of thing that properties are meant to prevent, but by deleting or otherwise changing the property, people can override my behavior like shown above.</p>

<p>That leads me to my question: should I just use a non-public validating setter method inside the class and simply use it as the property setter, or should I use a public setter method because validation of new values for a non-public attribute isn't an intended use of properties? I'm trying to avoid code duplication, so those are really my only two options for allowing user access while also validating the value in the case of a property setter or setter method.</p>

<p>Does anybody have any experience with this sort of issue? How did you resolve it, if you did?</p>

<p>Okay, I'm not sure there is a good solution here, so I'm going with a different option: if a user tries to do something that messes everything up, it's their own fault. After all, Python is "a language for consenting adults".</p>

<p>Just because an attribute is conventionally "public" by PEP 8 standards, that does not imply that a user of a class can just do whatever they want with its attributes, whether that's replacing their values, altering them in place, or even deleting them. That naturally goes for properties, too, which are class attributes.</p>

<p>I'll leave this unanswered for a while, but I'm fairly certain this attitude is the right way to approach this "problem".</p>

python properties