append
: Appends object at the end.
x = [1, 2, 3]
x.append([4, 5])
print(x)
gives you: [1, 2, 3, [4, 5]]
extend
: Extends list by appending elements from the iterable.
x = [1, 2, 3]
x.extend([4, 5])
print(x)
gives you: [1, 2, 3, 4, 5]
A mixin is a special kind of multiple inheritance. There are two main situations where mixins are used:
- You want to provide a lot of optional features for a class.
- You want to use one particular feature in a lot of different classes.
For an example of number one, consider werkzeug's request and response system. I can make a plain old request object by saying:
from werkzeug import BaseRequest
class Request(BaseRequest):
pass
If I want to add accept header support, I would make that
from werkzeug import BaseRequest, AcceptMixin
class Request(AcceptMixin, BaseRequest):
pass
If I wanted to make a request object that supports accept headers, etags, authentication, and user agent support, I could do this:
from werkzeug import BaseRequest, AcceptMixin, ETagRequestMixin, UserAgentMixin, AuthenticationMixin
class Request(AcceptMixin, ETagRequestMixin, UserAgentMixin, AuthenticationMixin, BaseRequest):
pass
The difference is subtle, but in the above examples, the mixin classes weren't made to stand on their own. In more traditional multiple inheritance, the AuthenticationMixin
(for example) would probably be something more like Authenticator
. That is, the class would probably be designed to stand on its own.
Best Answer
Numpy matrices are strictly 2-dimensional, while numpy arrays (ndarrays) are N-dimensional. Matrix objects are a subclass of ndarray, so they inherit all the attributes and methods of ndarrays.
The main advantage of numpy matrices is that they provide a convenient notation for matrix multiplication: if a and b are matrices, then
a*b
is their matrix product.On the other hand, as of Python 3.5, NumPy supports infix matrix multiplication using the
@
operator, so you can achieve the same convenience of matrix multiplication with ndarrays in Python >= 3.5.Both matrix objects and ndarrays have
.T
to return the transpose, but matrix objects also have.H
for the conjugate transpose, and.I
for the inverse.In contrast, numpy arrays consistently abide by the rule that operations are applied element-wise (except for the new
@
operator). Thus, ifa
andb
are numpy arrays, thena*b
is the array formed by multiplying the components element-wise:To obtain the result of matrix multiplication, you use
np.dot
(or@
in Python >= 3.5, as shown above):The
**
operator also behaves differently:Since
a
is a matrix,a**2
returns the matrix producta*a
. Sincec
is an ndarray,c**2
returns an ndarray with each component squared element-wise.There are other technical differences between matrix objects and ndarrays (having to do with
np.ravel
, item selection and sequence behavior).The main advantage of numpy arrays is that they are more general than 2-dimensional matrices. What happens when you want a 3-dimensional array? Then you have to use an ndarray, not a matrix object. Thus, learning to use matrix objects is more work -- you have to learn matrix object operations, and ndarray operations.
Writing a program that mixes both matrices and arrays makes your life difficult because you have to keep track of what type of object your variables are, lest multiplication return something you don't expect.
In contrast, if you stick solely with ndarrays, then you can do everything matrix objects can do, and more, except with slightly different functions/notation.
If you are willing to give up the visual appeal of NumPy matrix product notation (which can be achieved almost as elegantly with ndarrays in Python >= 3.5), then I think NumPy arrays are definitely the way to go.
PS. Of course, you really don't have to choose one at the expense of the other, since
np.asmatrix
andnp.asarray
allow you to convert one to the other (as long as the array is 2-dimensional).There is a synopsis of the differences between NumPy
arrays
vs NumPymatrix
es here.