[Tutor] instance method call issue
abasiemeka at gmail.com
Sun Oct 14 14:34:26 CEST 2012
Firstly, thank you so much for all the assistance you provide so
selflessly through this medium.
I come from a functional programming background and have only recently
become serious with OOP.
For practice, I tried to implement John Conways 'Game of Life' without
a GUI and ran into some issues.
I understand instance, static and class methods but I'm still
struggling with when and where they should be used.
In the code below, I can't seem to get around calling an instance
method (__reality_check()) from within a class method (update_grid()),
I'm not even too sure that update_grid() should have been a class
method in the first place.
Please, I need help with this and any other advice on my coding style,
where I could've been more efficient etc.
'''Grid(rows = 26, columns = 26) -> Provides a world object
for the cells in Game of Life'''
from time import sleep
from os import system
from sys import platform
__rows, __array, os_type = [''], , platform
def display_grid(generation, os_type):
(lambda x:Grid.system('cls') if 'win' in
os_type else Grid.system('clear'))(1)
print 'Cannot display Grid'
print 'Now in Generation %d' %(generation)
for line in Grid.__array:
def extend_grid(thickness = 4, force_extend = False):
'''extend_grid([thickness][, force_extend]) -->
Extends the edges of the grid by 4 units in
all directions if the boundary of the simulation comes close to the
edge. Extends anyway if force_extend option is True'''
def update_grid(cls, generations, speed):
'''Runs the 'Life' World simulation for the specified
number of generations at the given speed'''
#control = input
for times in range(generations + 1):
def __init__(self, rows = 26, col = 26):
'''Create a 'rows' x 'col' grid'''
Grid.__rows *= rows
Grid.__array = [Grid.__rows] * col
print 'A %d x %d cell World has been Created' %(rows, col)
def __reality_check(self, num = 'all'):
'''__reality_check([num]) -->Checks and updates the
state of the cell based on the number of 'alive' neighbors'''
#check if reality check is for single cell or entire array
if num == 'all':
for i in range(len(Grid.__array)):
for j in range(len(Grid.__array[i])):
#Neighbor count check and update for single cell(num = row, col)
elif num == tuple and len(num) == 2:
col, row, neighbor_count = num, num, 0
for x in range(row-1, row+2):
for y in range(col-1, col+2):
#count only 'alive' neighbors
(includes the subject cell itself)
neighbor_count = lambda x: x+1
if Grid.__array[x][y] == '##' else x
#correct the neighbor count value if subject
cell was counted
neighbor_count = lambda x: x-1 if
Grid.__array[row][col] == '##' else x
#update cell(x,y) state
Grid.__array[row][col] = lambda x:'##' if
neighbor_count == 3 else ''
print 'Wrong argument for reality check'
def edit_cell(self, cells, state = '##'):
'''edit_cell(cells[, state]) --> Where cells is a list
of tuples containing coordinates of the cells to edit'''
cells = cells
for eachcell in cells:
Grid.__array[eachcell][eachcell] = state
#To be moved to a different .py file after debugging.
world = Grid()
world.edit_cell([(10,10), (10, 11), (10, 12), (11, 10)], '##')
p.s. I'm not yet completely through with coding some aspects like the
More information about the Tutor