pass object or use self.object?

Xavier Ho contact at xavierho.com
Tue Apr 6 10:11:38 EDT 2010


> So my question is whether it's bad practice to set things up so each
> method operates on self.document or should I pass document around from
> one function to the next?
>

I think this depends on the use case.

If the functions you're calling in between have a chance to be called
independently, then I would probably allow passing the documents in the
arguments, so that other code can utilise this function. On the other
extreme, if this function is only used internally, I would simply use
self.object to avoid copying extra references, and treat them as sub
routines. I would also previx two underscores (__) before the function to
indicate that it's supposed to be interal-use only.

The other use case is, if self.object is useful after the function, I would
keep it as that. Then, other code could use it without processing it again.
On the other hand, if it is useless after created and returned, then I would
not keep a reference in the class object.

All in all, you probably won't find a big performance difference, so it all
boils down to logical placement.

Cheers,
Xav
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100407/9a88f0d5/attachment-0001.html>


More information about the Python-list mailing list