[Numpy-discussion] classmethods for ndarray

Robert Kern robert.kern@gmail....
Thu Feb 1 19:06:03 CST 2007


Christopher Barker wrote:
> Sebastian Haase wrote:
> 
>> Could you explain what a possible downside of this would be !?
>> It seems that if you don't need to refer to a specific "self" object
>> that a class-method is what it should - is this not always right !?
> 
> Well, what these really are are alternate constructors. I don't think 
> I've seen class methods used that way, but then I haven't seen them used 
> much at all.

Alternate constructors is probably the primary use case for class methods that
I've seen. It's certainly the most frequent reason I've made them.

> Sometimes I have wished for an overloaded constructor, i.e.:
> 
> array(SomeBuffer)
> 
> results in the same thing as
> 
> frombuffer(SomeBuffer)
> 
> 
> but Python doesn't really "do" overloaded methods, and there are some 
> times when there wouldn't be only one way the input could be interpreted.

Well, array() is already very, very overloaded. That's why it's difficult to use
sometimes. BTW, you might want to check out the module simplegeneric for a good
way to implement certain kinds of overloading. Just not numpy.array(), please ;-).

  http://cheeseshop.python.org/pypi/simplegeneric/

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco


More information about the Numpy-discussion mailing list