What you want is a step from Structured Programming to Object Oriented Programming in the syntax sense. The older code may be designed using OO principles (or OO approach can be simply natural). Firstly, the bl
must be the object if the second form is to be used. An object is known also as an instance of a class. This way, you have to define your class.
Then, bl
variable represents the object. In Python, it is the reference to the object. The object is created when you call the class name. The bl
is the name of the object from outside the class definition. The name of the same object from inside the class definition is named self
by convention.
To put it together, your function will be converted to member function of the class, and you should probably only rename the arr
(the first argument in the old function) in the old function definition to self
(to stick with the conventions). However, if bl
(from outside of the function definition, or arr
from inside of the function definition) was a list in the old code, it becomes an object reference. Think about the object as about a thin wrapper around your list. There are basically two ways to do that (composition and inheritance). Usually, the composition should be the prefered way when you do such conversion (unless you know what you are doing). It means that the object must store what was earlier the reference to the list. In the class definition, you will use self.arr
(or more suitable name) as the member variable for storing the reference to the list. To do that, you have to implement a special method (a method is also named as a member function) named __init__
(the underscores are doubled):
class MyArray:
def __init__(self, arr):
self.arr = arr
In Python, self.
prefix must be always used explicitly inside the class definition if you want to refere to member variables or member functions.
Now add your getColN
:
def getColN(self, n):
... change your body slightly...
Say you had def getColN(arr, n)
in your existing function. Then arr
was the reference to the list of lists. But now, the first argument of the function must be the reference to the object (i.e. class instance), and the array is now named self.arr
. This way, you should rename your earlier arr
in the function body to the self.arr
. Now you have:
class MyArray:
def __init__(self, arr):
self.arr = arr
def getColN(self, n):
... changed body...
You can use the new class definition to wrap the existing list of lists in the sense you pass the old structure to the constructed object:
x = MyArray(bl)
The x
will be your new replacement for the old bl
. The old bl
is passed to the __init__
-- seen as arr
from inside the class definition, and saved as self.arr
.
And you can call:
bln = x.getColN(n)
I did use the x
identifier to emphasize the difference only. When refactoring (rewriting the old code), you may want to prefer to keep the bl
identifier -- to reuse it for a different purpose:
bl = MyArray(bl)
The bl
as argument was the list of lists, but later it becomes the object reference.
When everything works, you can add implementation of other special methods, and you can easily use syntax for your object of your own class as if it was an array (here using x
again to emphasize the idea):
bln = x[n]