__init__ style questions

Will McGugan will at willmcgugan.com
Mon Oct 2 13:21:08 CEST 2006

Duncan Booth wrote:

> No it isn't Pythonic. Why not just require 3 values and move the
> responsibility onto the caller to pass them correctly? They can still use
> an iterator if they want:
>     Vector3D(a, b, c)
>     Vector3D(*some_iter)

I kind of liked the ability to partially use iterators. It would be
convenient for reading in from a file for example

f = file( "model.txt" )
v1 = Vector3D( f )
v2 = Vector3D( f )
v3 = Vector3D( f )

Which you couldnt do with a tuple, because the * syntac would attempt
to read the entire file (I think).

> Then your initialiser becomes:
> def __init__(self, x=0, y=0, z=0):
>     self.x, self.y, self.z = x, y, z
> much cleaner and also catches accidental use of iterators.
> Alternatively, insist on always getting exactly 0 or 1 arguments:
>     Vector3D((a,b,c))
>     Vector3D(some_iter)
> def __init__(self, (x, y, z)=(0,0,0)):
>     self.x, self.y, self.z = x, y, z
> which is great if you already have lots of 3-tuples, but a pain otherwise
> to remember to double the parentheses.

Hmm. Not keen on that for the reason you mentioned. For my particular
use case there would be a lot of Vector3D 'literals'.

More information about the Python-list mailing list