Like overloading __init__(), but how?
John M. Gabriele
john_sips_teaz at yahooz.com
Thu Feb 24 00:56:23 EST 2005
On Wed, 23 Feb 2005 21:32:52 -0700, Steven Bethard wrote:
> [snip]
> Another possibility is to play around with *args:
>
> class Vector3d(object):
> def __init__(self, *args):
> if not args:
> # constructor with no arguments
> elif len(args) == 6:
> # constructor with xyz coords
> elif len(args) == 2:
> # constructor with points
> elif len(args) == 1:
> # copy constructor
> else:
> raise TypeError('expected 0, 1, 2 or 6 args, got %i' %
> len(args))
>
> But this can get really ugly really quick.
>
> HTH,
>
> STeVe
Thanks STeVe. I think, for now, I'll probably stick with
the easier but less elegant way of checking *args. I guess
I could compare args[0].__class__.__name__ against 'Point3d',
'Vector3d', or just 'float'.
Didn't realize this topic came up recently. Sorry for the
duplication.
---J
--
--- if replying via email, remove zees ---
More information about the Python-list
mailing list