On Sat, Jun 28, 2014 at 03:55:00PM -0400, Ben Hoyt wrote:
Re is_dir etc being properties rather than methods:
The problem with this is that properties "look free", they look just like attribute access, so you wouldn't normally handle exceptions when accessing them. But .lstat() and .is_dir() etc may do an OS call, so if you're needing to be careful with error handling, you may want to handle errors on them. Hence I think it's best practice to make them functions().
I think this one could go either way. Methods look like they actually re-test the value each time you call it. I can easily see people not realising that the value is cached and writing code like this toy example:
# Detect a file change. t = the_file.lstat().st_mtime while the_file.lstat().st_mtime == t: sleep(0.1) print("Changed!")
I know that's not the best way to detect file changes, but I'm sure people will do something like that and not realise that the call to lstat is cached.
Personally, I would prefer a property. If I forget to wrap a call in a try...except, it will fail hard and I will get an exception. But with a method call, the failure is silent and I keep getting the cached result.
Speaking of caching, is there a way to freshen the cached values?