OO conventions
Alex Martelli
aleaxit at yahoo.com
Thu Feb 2 11:03:27 EST 2006
Magnus Lycka <lycka at carmen.se> wrote:
> Alex Martelli wrote:
> > Steven D'Aprano <steve at REMOVETHIScyber.com.au> wrote:
> >>And now, at long last, the image object actually is an image. So why make
> >>this a two step process? Whatever the Image() initialization does, why
> >>can't it be done automatically when you read the file?
> >
> > "Two-step construct" ("2SC") is a reasonably well-known and widely
> > useful idiom, and it can serve several kinds of purposes, many but not
> > all of which are tied to persistence.
>
> I've seen it used as a C++ coding standard when e.g. loading
> instances from databases (Person p=Person(); p.init(id);)
> since it was considered bad to have code in the constructor
> which could throw exceptions. If you make a new Person on
> the heap, and catch an exception from the constructor, are
> you sure that you delete the object?
Yes, this is a language-specific issue that applies to C++. But I tried
to show that 2SC (and its counterpart, 2SD) can come in handy in many
other languages, too, including Python.
Alex
More information about the Python-list
mailing list