constructor classmethods

stestagg at gmail.com stestagg at gmail.com
Wed Nov 2 09:46:25 EDT 2016


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



More information about the Python-list mailing list