constructor classmethods

breamoreboy at gmail.com breamoreboy at gmail.com
Wed Nov 2 10:06:20 EDT 2016


On Wednesday, November 2, 2016 at 1:47:00 PM UTC, stes... at gmail.com wrote:
> Hi
> 
> I was hoping to canvas opinion on using classmethods as constructors over __init__.
> 
> We've got a colleague who is very keen that __init__ methods don't contain any logic/implementation at all, and if there is any, then it should be moved to a create() classmethod.
> 
> As a concrete example, one change proposed was to go from this:
> 
> ```
> def __init__(self, ...):
>     self.queue = Queue.Queue()
> 
> to this:
> 
> def __init__(self, queue):
>     self.queue = queue
> 
> @classmethod
> def create(cls, ...):
>     return cls(Queue.Queue())
> 
> ```
> 
> The rationale is that it decouples the class from the queue implementation (no evidence or suggestion that we would actually ever want to change impl in this case), and makes testing easier.
> 
> I get the underlying principal, and it's one that a strict OOp approach would suggest, but my gut feeling is that this is not a pythonic approach at all.
> 
> What do people feel about this?
> 
> Thanks
> 
> Steve

I suggest that you read this https://docs.python.org/3/reference/datamodel.html#object.__new__ before you do anything.

Kindest regards.

Mark Lawrence.



More information about the Python-list mailing list