Maybe a bit of example code will help: Notice the difference in the call signatures of foo
, class_foo
and static_foo
:
class A(object):
def foo(self, x):
print(f"executing foo({self}, {x})")
@classmethod
def class_foo(cls, x):
print(f"executing class_foo({cls}, {x})")
@staticmethod
def static_foo(x):
print(f"executing static_foo({x})")
a = A()
Below is the usual way an object instance calls a method. The object instance, a
, is implicitly passed as the first argument.
a.foo(1)
# executing foo(<__main__.A object at 0xb7dbef0c>, 1)
With classmethods, the class of the object instance is implicitly passed as the first argument instead of self
.
a.class_foo(1)
# executing class_foo(<class '__main__.A'>, 1)
You can also call class_foo
using the class. In fact, if you define something to be
a classmethod, it is probably because you intend to call it from the class rather than from a class instance. A.foo(1)
would have raised a TypeError, but A.class_foo(1)
works just fine:
A.class_foo(1)
# executing class_foo(<class '__main__.A'>, 1)
One use people have found for class methods is to create inheritable alternative constructors.
With staticmethods, neither self
(the object instance) nor cls
(the class) is implicitly passed as the first argument. They behave like plain functions except that you can call them from an instance or the class:
a.static_foo(1)
# executing static_foo(1)
A.static_foo('hi')
# executing static_foo(hi)
Staticmethods are used to group functions which have some logical connection with a class to the class.
foo
is just a function, but when you call a.foo
you don't just get the function,
you get a "partially applied" version of the function with the object instance a
bound as the first argument to the function. foo
expects 2 arguments, while a.foo
only expects 1 argument.
a
is bound to foo
. That is what is meant by the term "bound" below:
print(a.foo)
# <bound method A.foo of <__main__.A object at 0xb7d52f0c>>
With a.class_foo
, a
is not bound to class_foo
, rather the class A
is bound to class_foo
.
print(a.class_foo)
# <bound method type.class_foo of <class '__main__.A'>>
Here, with a staticmethod, even though it is a method, a.static_foo
just returns
a good 'ole function with no arguments bound. static_foo
expects 1 argument, and
a.static_foo
expects 1 argument too.
print(a.static_foo)
# <function static_foo at 0xb7d479cc>
And of course the same thing happens when you call static_foo
with the class A
instead.
print(A.static_foo)
# <function static_foo at 0xb7d479cc>
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]
Best Answer
1. The meaning of shapes in NumPy
You write, "I know literally it's list of numbers and list of lists where all list contains only a number" but that's a bit of an unhelpful way to think about it.
The best way to think about NumPy arrays is that they consist of two parts, a data buffer which is just a block of raw elements, and a view which describes how to interpret the data buffer.
For example, if we create an array of 12 integers:
Then
a
consists of a data buffer, arranged something like this:and a view which describes how to interpret the data:
Here the shape
(12,)
means the array is indexed by a single index which runs from 0 to 11. Conceptually, if we label this single indexi
, the arraya
looks like this:If we reshape an array, this doesn't change the data buffer. Instead, it creates a new view that describes a different way to interpret the data. So after:
the array
b
has the same data buffer asa
, but now it is indexed by two indices which run from 0 to 2 and 0 to 3 respectively. If we label the two indicesi
andj
, the arrayb
looks like this:which means that:
You can see that the second index changes quickly and the first index changes slowly. If you prefer this to be the other way round, you can specify the
order
parameter:which results in an array indexed like this:
which means that:
It should now be clear what it means for an array to have a shape with one or more dimensions of size 1. After:
the array
d
is indexed by two indices, the first of which runs from 0 to 11, and the second index is always 0:and so:
A dimension of length 1 is "free" (in some sense), so there's nothing stopping you from going to town:
giving an array indexed like this:
and so:
See the NumPy internals documentation for more details about how arrays are implemented.
2. What to do?
Since
numpy.reshape
just creates a new view, you shouldn't be scared about using it whenever necessary. It's the right tool to use when you want to index an array in a different way.However, in a long computation it's usually possible to arrange to construct arrays with the "right" shape in the first place, and so minimize the number of reshapes and transposes. But without seeing the actual context that led to the need for a reshape, it's hard to say what should be changed.
The example in your question is:
but this is not realistic. First, this expression:
computes the result more simply. Second, is there really something special about column 0? Perhaps what you actually need is: