[Python-Dev] Proposal for Python 3.3: dependence injection
Antoine Pitrou
solipsis at pitrou.net
Thu Mar 24 17:57:58 CET 2011
On Thu, 24 Mar 2011 11:46:42 -0500
Benjamin Peterson <benjamin at python.org> wrote:
> 2011/3/24 Jesus Cea <jcea at jcea.es>:
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1
> >
> > Hi, everybody.
> >
> > I want to test the dev community interest in modifying the stdlib to
> > ease dependence injection.
>
> I, for one, am -1. Code shouldn't be uglified for the purposes of
> testing. It's also a slippery slope. Maybe we should add parameters
> for open() and io functions? What about sys and os? Those often need
> to be mocked.
A non-ugly way of doing "dependency injection" is to rely on
class/instance attributes instead. e.g.:
class FTP:
socket_factory = socket.create_connection
def open(self, host, port):
self.sock = self.socket_factory(host, port)
...
Regards
Antoine.
More information about the Python-Dev
mailing list