def __init__(self):
Marko Rauhamaa
marko at pacujo.net
Tue Apr 26 04:25:39 EDT 2016
Ben Finney <ben+python at benfinney.id.au>:
> Gary Herron <gherron at digipen.edu> writes:
>
>> The __init__ method is the constructor for instances of a class.
>> It is not required, but the situations in which a constructor is
>> not needed are few and unusual.
>
> That's needlessly confusing: ‘__init__’ is not a constructor because
> it does not construct the instance. The ‘__new__’ method is the
> constructor for a class (and returns the new instance).
I have never ever had a temptation to specify a __new__ method. I can't
imagine a *beneficial* case of overriding it.
> The ‘__init__’ method requests the already-constructed instance to
> initialise itself (and returns None).
It is a serious practical problem that an object can't guarantee that
its __init__ has been called.
Check out some of the stdlib source code for example:
========================================================================
class ThreadPoolExecutor(_base.Executor):
def __init__(self, max_workers):
"""Initializes a new ThreadPoolExecutor instance.
Args:
max_workers: The maximum number of threads that can be used to
execute the given calls.
"""
self._max_workers = max_workers
self._work_queue = queue.Queue()
self._threads = set()
self._shutdown = False
self._shutdown_lock = threading.Lock()
========================================================================
Notice how _base.Executor.__init__(self) does not get called. It can
only work if _base.Executor does not specify an __init__. That's an
assumption you really couldn't make with a clear conscience even if you
were the author and maintainer of both modules
(concurrent.futures.thread and concurrent.futures._base).
Marko
More information about the Python-list
mailing list