__init__ is the initialiser

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Feb 1 00:23:08 EST 2014


On Fri, 31 Jan 2014 22:16:59 -0500, Terry Reedy wrote:

> Creating a painting on canvas has two similar phases. Prepare a generic
> blank canvas stretched on a frame and coated with a white undercoat.
> Paint a particular picture. Would you say that the second step is not
> creating anything?

A dubious analogy, since there are artists who would say that attacking 
the canvas with a knife and setting the remains on fire count as a form 
of artistic creation :-)

"Creation" can mean various things in English. One might argue that the 
only creation was the guy at the factory who made the canvas, and the 
people who made the paints. After that comes the preparation stage, 
followed by the splashing-paint-on-canvas stage.

If you want to use "creating" to refer to the splashing-paint stage, then 
I think the closest analogy in Python is the stuff which happens *after* 
__init__ is called. Using a class often means modifying it as you go (in 
much the same way that using a canvas requires modifying it as you go), 
and it's never *completely* prepared until just before you stop using it. 
One adds data, modifies the data, moves data from one attribute to 
another, sets up temporary data attributes, calls methods which modify 
the instance, and finally get the final answer you want, at which point 
your task is done and the instance is thrown away. Think of a dict:

d = {}  # Dict is constructed.
for key in data:
    key = process(key)
    if condition():
        d[key] = something(key)


Is all that work also not part of the object construction process? If we 
write it like this:

d = dict(something(key) for key in map(process, data) if condition())

you'd probably call it the constructor. But the two bits of code do the 
same thing.

While we talk about a constructor as if the process of constructing an 
object was black and white with a clear and obvious dividing line, in 
practice its often fuzzy, with a lot of grey. (That's one of the 
objections the functional crowd has with mutable objects.)

So to answer your question: no, in context of programming, I would *not* 
call the putting-paint-to-canvas stage of artist painting to be 
"creation". I would call it *using* the constructed object (the canvas). 
Whether that *use* of the canvas happens to be *artistic* creation or 
mere derivative pap is another story :-)



-- 
Steven



More information about the Python-list mailing list