On Mon, Jun 1, 2009 at 8:59 AM, Alan Gauld <[email protected]>wrote:
> > "W W" <[email protected]> wrote > > ( Multiple constructors (or factory methods) is one feature I would like >>> to see added to Python! ) >>> >> >> Wouldn't it be possible to create sort of a... bastardization? i.e. >> >> def __init__(self, *args): >> if len(args) == 0: >> #do something >> if len(args) == 1: >> #do something else >> >> Or would that create more problems than is worth it? >> > > Thats the standard workaround but its not clean and certainly > not self documenting. Its also not reliable becaause thre is nothing > to stop someone calling it with the first argument of the 2 arg case > (a filename maybe?) and then it gets treated as a single arg case > ( a tuple of values say?) Oops! > > Whereas in C++/Java style you could say > > class C: > @constructor > def C(fname, count);... > > @constructor > def C(valueTuple):.... > > etc. > > Or in Delphi style: > > class C: > @constructor > def LoadFromFile(fname, count): ... > @constructor > def Create(valueTuple):... > > Personally I prefer the Delphi style sincve it makes the constructor > call explicit and adds to the documentation. > > Alan G > That does make it problematic... although I suppose checking the type would be a workaround - still, not simple or beautiful. Has this been introduced as a PEP? -Wayne
_______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
