Hello everyone, I'll step into conversation too as I think it is quite 
important topic. I'd be the one my collegue calls keen to this practice.

Little bit background related to this topic. It all starts from this article:
http://misko.hevery.com/attachments/Guide-Writing%20Testable%20Code.pdf

The guide is written in c++ in mind, yet the concepts stands for any 
programming language really. Read it through and think about it. If you come 
back to this topic and say: "yeah, but it's c++", then you haven't understood 
it. 

Steven is correct in his post, this is Dependency Injection (thanks for seeing 
through my suggestion, I was starting to think I'm completely crazy, now I'm 
just partially crazy). Create classmethod is mostly actually for convenience, 
especially in those cases where the class would require injecting multiple 
dependencies, which doesn't need to come from outside. I'm fine actually fine 
using default parameters. I'd like to use __new__ and __init__ here, but I 
haven't figured out how to do it with those in a way it's comfortable to write 
and read, which is usually a key point to sell a new practice and also probably 
a reason why create-classmethod is not popular eiher, regardless it being 
useful.

My key goal is to maximize the control of object creation, which makes writing 
tests much much simpler. This is not necessarily OOP, and definitely not strict 
of such. It comes from the above article and that it is following pretty 
strictly in a sense. I have at least found it very useful practice to have no 
logic in constructors. Mock library is definitely powerful mechanism to solve 
some probelms in Python for testability, but it can (if not carefuly how to 
write classes you are going to test) make tests very hard to read and if you 
want to treat your tests as documentation, it is unacceptable.

In order to write a code that just solves the problem, none of those patterns 
are needed. To solve a problem in a manner that it's possible to return to it 
later on, then these patterns start to matter.

Br,
Teppo
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to