In numpy, why is it ok to do matrix.mean(), but not ok to do matrix.median()?

Chris Angelico rosuav at gmail.com
Tue May 1 22:37:12 EDT 2018


On Wed, May 2, 2018 at 12:22 PM, C W <tmrsg11 at gmail.com> wrote:
> It's interesting how mean() can be implemented, but median() will break
> other packages.
>
> So, the default way in numpy is to use functions, not methods?
>
> When I first learned Python, I was told to create an object and to play
> around, there are methods for that object. List has list methods, tuple has
> tuple methods, etc.
>
> Now, the default way in numpy is to use function instead of methods? I'm
> confused. What happened to object-oriented programming?

Even outside of numpy, a lot of Python uses functions, not methods.
One good reason for this is that a function can accept a wide variety
of data types as its argument; for instance, len() accepts many
things, not just lists. Some things are done with methods, others with
stand-alone functions. There are design choices each way.

ChrisA



More information about the Python-list mailing list