code style and readability [was: Re: Checking the boolean value of a collection]

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sat Sep 13 16:56:45 CEST 2008


Larry Bates a écrit :
(snip)
> IMHO it reads better if you use the __call__ method of the class to 
> return the value 

IMHO, it makes no sense at all to abuse the __call__ magic method here.

> and rewrite it as a regular loop for clarity.  
> Sometimes the simplest way is the easiest to read.
> 
> class FolderInUse:
>    def __call__(self, archivefolder):
>        result = False
>        for instance in self.core.active_outgoing_registration_instances():
>            if instance.forbid_to_close(archivefolder):
>                result = True
>                break
> 
>        return result


the loop would be simpler with an early return:


   for instance in self.core.active_outgoing_registration_instances():
     if instance.forbid_to_close(archivefolder):
       return True
   return False

> 
> 
> Then it can be called with:
> 
>     if FolderInUse(archivefolder):
>         ...

This will call the class constructor and initializer and return a new 
instance, not the *instance method* __call__.




More information about the Python-list mailing list