Generate a new object each time a name is imported

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon Aug 3 23:30:35 EDT 2009


On Mon, 03 Aug 2009 16:38:43 +0200, Jean-Michel Pichavant wrote:


> So what's the purpose of making
> 
>>from Module import factory as a
>>from Module import factory as b
> 
> return 2 different objects ? If I had to write this code I would expect
> 'a is b' to return 'True'.
> 
> This is no "don't do that" answer, it's a sincere question: what  is the
> benefit of your /new/ syntax ?


Consider it "properties for modules":

a = obj.factory
b = obj.factory

doesn't promise that a is b, because factory might be a property that 
returns a new object each time.

Actually, this behaviour pre-dates properties. If obj has a __getattr__ 
or __getattribute__ method, then it could do the same thing.


-- 
Steven



More information about the Python-list mailing list