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

Lie Ryan lie.1296 at gmail.com
Sat Aug 23 15:50:53 CEST 2008


On Sat, 2008-08-23 at 12:00 +0200, tutor-request at python.org wrote:
> 
> Message: 1
> Date: Fri, 22 Aug 2008 20:46:25 -0400
> From: "lawful falafel" <lawfulfalafel at gmail.com>
> Subject: [Tutor] Problem with creating a class to access a 2d array
> To: tutor at python.org
> Message-ID:
>         <c22a4930808221746w65f2a576v2c44185ea94c860c at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> Hello,
> 
> I am currently trying to teach myself python. This isnt my first
> language,
> as I used to be quite good at using java. But it seems to be a bit
> harder
> for me to understand some stuff. Right now, in trying to teach myself
> the
> language, I thought I would make a simple text based game, like Zork
> or
> Adventure or whatever. Anyway, right now I am trying to create the map
> generating function, and I am not exactly sure what to do.
> 
> Here is all of the code I have so far:
> 
> ------------
> 
> class Map():
>     def __init__(self):
>         self.make_map()

__init__ can take arguments, like this:
    def __init__(self, x, y):
        self.make_map(x, y)

>     def make_map():

You've got to receive self[1] on EVERY instance method. This is because
python's instance method access is actually like this:

class A(object): 
    def instance_method(self, args):
        print args
a = A()

# this
a.instance_method(args)
# is translated into this
A.instance_method(a, args)

[1] Actually you can use any valid python name in place of self, but the
use of the name self is an extremely strong convention

>         #first create array to store data
>         map = [][]

that is a syntax Error.

>         for x in range(10):
>             for y in range(10):
>                 map[x][y] = map_random()

In python, your variable 'map' and 'map_random' are local names that are
accessible only within the function and destroyed when the function
exits. To access and manipulate an instance variable, which is
accessible within the instance, use self.map and self.map_random, this
is similar to C's (and I think Java's) "this".

>     def map_random():
>         #This would do something simple, such as deciding whether
>         #this is grass or gravel terrain and if there is an object
>         #nearby

In python, the module to use is random
import random
print random.randrange(0, 100) # 0-99, just like range()
print random.randint(0, 100) # 0-100
print random.choice(['grass', 'gravel', 'mud'])

> -------------
> 
> 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. 

Simply because it is usually easier to explain things in interactive
mode. I found that most tutorials that explain things in source code
file is usually unable to explain things in an easy to understand manner
(e.g. Microsoft's MSDN). Sure there are a few good ones, most not.

> So basically right now I dont even
> know what equivelent version of main() I would write. 

You don't write main() function in python (although you can if you
insist).

A simple python program would usually just start with codes on the root
level:

print 'Hello, this is a simple program'
print 'Enter A or B'
answer = raw_input()
if answer == 'A':
    print 'you answered A'
elif answer == 'B":
    print 'you answered B'
else:
    print 'I don't understand'
print 'Bye'

In a more complex program, you usually have an initial function to be
run (i.e. main-like function)

# define a function
def main():
    print 'Hello, this is a simple program'
    print 'Enter A or B'
    answer = raw_input()
    if answer == 'A':
        print 'you answered A'
    elif answer == 'B":
        print 'you answered B'
    else:
        print 'I don't understand'
    print 'Bye'

# run the function
main()
print 'Exiting'

but this could be problematic if your program is intended to be
imported, so this boilerplate of __name__ == '__init__' has appeared:

# define a function
def main():
    print 'Hello, this is a simple program'
    print 'Enter A or B'
    answer = raw_input()
    if answer == 'A':
        print 'you answered A'
    elif answer == 'B":
        print 'you answered B'
    else:
        print 'I don't understand'
    print 'Bye'

# run the function
if __name__ == '__main__':
    main()
    print 'Exiting'

An even more complex program wouldn't just have a main() function, but a
"main" object

> 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. 

Why do you think so? There is a reason it's called __init__ not __new__,
it's because, unlike a constructor[2], __init__'s job is not to return a
new object of that class, but to initialize the instance to a usable
state

[2] In python, there is a constructor: __new__, but because of the
design of python most programs wouldn't need to touch it.

> Any help or links to sites
> about python and OO stuff would really be appreciated.



More information about the Tutor mailing list