Which style is preferable?

Lonnie Princehouse finite.automaton at gmail.com
Fri Jul 16 22:35:13 CEST 2004


Why do type checking at all?  

Element-wise addition will raise an exception if you're trying to add
two elements that can't be added. If you insist on type checking, then
mechanisms like implicit conversion (i.e. int->float) and overloaded
operators won't have a chance to work their magic.

PS- Try making Vector a subclass of list:

class Vector(list):
  def __add__(self, other):
    return Vector([ a + b for a, b in zip(self, other)])

>> x = Vector([1.0, 2.0, 3.0])
>> y = Vector([4.0, 5.0, 6.0])
>> x + y
[5.0, 7.0, 9.0]

# Since we're not type checking, we can add a list to a vector:
>> Vector([1.0, 2.0]) + [100, 200]
[101.0, 202.0]

# And we can do all sort of other novel things
>> Vector('hello') + Vector('world')
['hw', 'eo', 'lr', 'll', 'od']



Antoon Pardon <apardon at forel.vub.ac.be> wrote in message news:<slrncffner.lnv.apardon at trout.vub.ac.be>...
> For the sake of this question I'm writing a vector class. So
> I have code something like the following
> 
> 
> from types import *
> import operator
> 
> 
>   class Vector:
>   
>     def __init__(self, iter):
>   
>       self.val = [ el for el in iter ]
>       for el in self.val:
>         if not isinstance(el, FloatType):
>           raise TypeError
>   
>   
>     def __add__(self, term):
>   
>       if ! isinstance(term, Vector):
>         raise TypeError
>       if len(self.val) != term.val:
>         raise ValueError
>       resval = map(operator.add, self.val, term.val)
>       return Vector(resval)
> 
> 
> The problem I have with the above code is, that I know
> that resval will contain only floats so that calling
> Vector with resval will needlessly do the type checking.
> 
> 
> So one way to solve this would be to have a helper
> function like this:
> 
>   def _Vec(lst)
>   
>     res = Vector()
>     res.value = lst
>     return res
> 
> 
> The last line of the __add__ method would then
> look like this.
> 
> 
>   return _Vec(resval)
> 
> 
> But I'm not really satified with this solution either
> because it needs a double call to generate an unchecked
> Vector.
> 
> 
> So I would prefer to write it like this:
> 
> 
>   def Vector(iter):
> 
>     lst = [ el for el in iter ]
>     for el in lst:
>       if not isinstance(el, FloatType):
>         raise TypeError
>     return _Vec(lst)
> 
> 
>   class _Vec(lst):
> 
>     def __init__(self, lst):
>       self.val = lst
> 
>     def __add__(self, term):
>   
>       if ! isinstance(term, _Vec):
>         raise TypeError
>       if len(self.val) != term.val:
>         raise ValueError
>       resval = map(operator.add, self.val, term.val)
>       return _Vec(resval)
> 
> 
> The only problem with this is that it is awkward
> to use with isinstance and such, so I would finaly
> add the folowing:
> 
>   VectorType = _Vec
> 
> 
> My question now is, would this be considered an acceptable
> style/interface whatever to do in python?



More information about the Python-list mailing list