pythonic use of properties?

Michael Spencer mahs at telcopartners.com
Sat Apr 16 17:04:51 EDT 2005


Marcus Goldfish wrote:

> 
> So what do you consider when making this decision

Python style tends away from validating what doesn't need to be validated.  The 
argument seems to be that the additional validating code comes at the price of 
legibility, and perhaps flexibility.

It's common in Python to use public attributes - even though an irresponsible 
user could set an object to a harmful or meaningless state.

Instead, the Python mantra is that it is 'better to ask forgiveness than 
permission', i.e., to handle exceptions gracefully rather than trying to avoid them.

One form of validation that is particularly discouraged in Python is type-checking.

That said, of course there are cases where data must be validated.  For data 
stored in one type, and used in many contexts - I would validate where stored. 
If the reverse, then validate at the point it is used.  In the likely case of 
'somewhere in between these extremes', personal taste and project conventions.


> and do these factors differ between python and C#/Java?
> 

I have rarely used C#, and never Java.

C#'s strongly typed method signatures imply a contract to act properly on 
objects passed to the method.  Given this, it seems sensible to me to include 
data validation in the source object.

Python's lack of type-checking of callable arguments invites the use of 'duck 
types'.  On the margin, I think, this argues for putting data validation at the 
point where the data is used, rather than when it is stored.


Michael




More information about the Python-list mailing list