What is the difference between mean and mean()? For eg, to create a copy of numpy array we use .copy() but to get the shape of the numpy array, we simply use .shape
copy()
is method and shape
is an attribute.
According to Python’s glossary:
attribute: A value associated with an object which is referenced by name using dotted expressions. For example, if an object
o
has an attributea
it would be referenced aso.a
method: A function which is defined inside a class body. If called as an attribute of an instance of that class, the method will get the instance object as its first argument (which is usually called self). See function and nested scope.
Mean
can function as either a function mean(ar)
or as a method ar.mean()
. But they do the same thing. Both prints an output of 3.0.
from numpy import array
list = [1, 2, 3, 4, 5]
ar = array(list)
print(mean(ar))
print(ar.mean())