Closed Thread
Results 1 to 2 of 2

Thread: Probably a simple class question

  1. #1
    utdiscant is offline Newbie
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Probably a simple class question

    Hi, i am quite new to python. I am trying (only for learning) to implement af simple grid-structure, which could be used as base for a simple 2d adventure game. My idea was to have a struct-like object which could hold 5x5 objects of the same type. Well, the code should reveal my idea. The problem is that when to code is executed, the output is:

    The world
    A simple table
    A simple table

    Which should be:

    The world
    A small house
    A simple table

    Code:
    class Grid:
    	"A grid class"
    	a = [[None for i in range(5)] for j in range(5)]
    	def __init__(self, des):
    		self.description = des		
    
    world = Grid("The world")
    
    world.a[0][0] = Grid("A small house")
    
    world.a[0][0].a[0][0] = Grid("A simple table")
    
    print world.description
    print world.a[0][0].description
    print world.a[0][0].a[0][0].description
    I think i might need to have a totally different approach here, and i am open to suggestions .

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    utdiscant is offline Newbie
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Re: Probably a simple class question

    I think i might have solved this problem myself.

    I thought an optimization would be to make the class definition like this:

    Code:
    class Grid:
    	"A grid class"
    	def __init__(self, des, width, height):
    		self.description = des
    		self.a = [[None for i in range(height)] for j in range(width)]
    And that solved the problem. Now i just need to know why

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. No output on simple code: learning ArrayList class
    By scottbomb in forum Java Help
    Replies: 3
    Last Post: 06-08-2011, 06:46 PM
  2. Beginner Tutorial: Creating a simple class with methods
    By mr mike in forum Ruby on RailsTutorials
    Replies: 0
    Last Post: 01-03-2011, 08:52 PM
  3. Simple Class Question
    By Namesake in forum C and C++
    Replies: 14
    Last Post: 02-09-2010, 05:32 PM
  4. Java abstract class - simple view of it.
    By Turk4n in forum Classes and Code Snippets
    Replies: 0
    Last Post: 01-06-2010, 10:15 AM
  5. class/class pointer question
    By MerakSpielman in forum C and C++
    Replies: 2
    Last Post: 04-12-2009, 09:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts