[Tutor] Problem with creating a class to access a 2d array

wesley chun wescpy at gmail.com
Sat Aug 23 09:00:40 CEST 2008

> I am currently trying to teach myself python. This isnt my first language,
> as I used to be quite good at using java.

you are in good shape... you will only need to "tweak" some of your
notions with regards to Java for Python, and you will be in good

> I really like python, but the one thing I really hate is that most of the
> tutorials treat it like interactive scripting instead of object oriented
> development, which really confuses me.

one of Python's strengths is that it can be used for both interactive
scripting as well as to develop fully object-oriented software
applications. tutorials tend towards the former only because Python is
also very popular as a first language for those completely new to
programming. i'm sure someone on this list can point you to a more
object-focused intro to Python.

> So basically right now I dont even
> know what equivelent version of main() I would write. I also dont understand
> how I could write an accessing method so that I could write Map(1,2), since
> the only place that deals with arguments is the __init__ function I think,
> and that doesnt seem to deal with arguments.

> class Map():
>     def __init__(self):
>         self.make_map()

minor syntax fix: change your class declaration to either "class Map:"
or "class Map(object):". then to instantiate, instead of using "Map
map = new Map();", you would do "map = Map()".

to tweak your initializer -- Python's aren't really "constructors" --
you can do this:

def __init__(self, x, y):
    self.make_map(x, y) # or whatever method you have that takes input

if you want x and y to be optional, you can set default values for x, y:

def __init__(self, x=0, y=0):
   # same as whatever you had

>     def make_map():
>         #first create array to store data
>         map = [][]
>         for x in range(10):
>             for y in range(10):
>                 map[x][y] = map_random()

in Python, you don't have to predeclare arrays. you could if you
wanted, by using a list comprehension for small 2-dim arrays (or
generator expressions if much larger)... here's an example to put in a
placeholder of None (which is like Java's null):

>>> [[None for y in range(10)] for x in range(10)]
[[None, None, None, None, None, None, None, None, None, None], [None,
None, ...<snip>... None, None, None]]

better yet, you can fill in whatever random value you wanted:

[[map_random() for y in range(10)] for x in range(10)]

here is another construct where you create pairs of 2-tuples, making
up a 1-dimension array rather than multidim:

>>> [(x,y) for x in range(10) for y in range(10)]
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0,
8), (0, 9), (1, 0), (1, 1), ...<snip>... (9, 8), (9, 9)]

in Core Python, i do give a pretty lengthy write up on doing OOP with
Python, so i'd invite you to take a look if you get a chance.

hope this helps, and welcome to Python!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001

"Python Web Development with Django", Addison Wesley, (c) 2008

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca

More information about the Tutor mailing list