Postpone creation of attributes until needed

Frank Millman frank at chagford.com
Tue Jun 12 01:35:46 EDT 2007


On Jun 12, 1:46 am, Steven D'Aprano
<s... at REMOVE.THIS.cybersource.com.au> wrote:
>
> >> You haven't told us what the 'compute' method is.
>
> >> Or if you have, I missed it.
>
> > Sorry - I made it more explicit above. It is the method that sets up
> > all the missing attributes. No matter which attribute is referenced
> > first, 'compute' sets up all of them, so they are all available for
> > any future reference.
>
> If you're going to do that, why not call compute() from your __init__ code
> so that initializing an instance sets up all the attributes?

Because, as I have tried to explain elsewhere (probably not very
clearly), not all the information required to perform compute() is
available at __init__ time.

I have gained a lot of valuable advice from this thread, but I do have
a final question.

Every respondent has tried to nudge me away from __getattr__() and
towards property(), but no-one has explained why. What is the downside
of my approach? And if this is not a good case for using
__getattr__(), what is? What kind of situation is it intended to
address?

Thanks

Frank




More information about the Python-list mailing list