NameError: name ‘self’ is not defined

Default argument values are evaluated at function define-time, but self is an argument only available at function call time. Thus arguments in the argument list cannot refer each other. It’s a common pattern to default an argument to None and add a test for that in code:

Understanding slice notation

It’s pretty simple really: There is also the step value, which can be used with any of the above: The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default). The other feature is that start or stop may be a negative number, which … Read more

Here’s a generator that yields the chunks you want: If you’re using Python 2, you should use xrange() instead of range(): Also you can simply use list comprehension instead of writing a function, though it’s a good idea to encapsulate operations like this in named functions so that your code is easier to understand. Python 3: Python 2 … Read more

Inverse of a matrix using numpy

The I attribute only exists on matrix objects, not ndarrays. You can use numpy.linalg.inv to invert arrays: Note that the way you’re generating matrices, not all of them will be invertible. You will either need to change the way you’re generating matrices, or skip the ones that aren’t invertible. Also, if you want to go through all 3×3 matrices with elements drawn … Read more