Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Namtaru

Pages: [1]
1
Programming / Re: python/libtcod tutorial, need help w/ list comprehensions
« on: February 01, 2013, 05:56:52 AM »
Thank you for that it makes a lot more sense now. I kept going through the tutorial yesterday and today, and after coding more and reading this thread I got it finally. Certain concepts feel like a wall until they turn into just another step  :) Speaking of which I have a few questions about class components... but that's for another thread when I am not too falling asleep at the keyboard.

Thanks again guys

2
Programming / python/libtcod tutorial, need help w/ list comprehensions
« on: January 29, 2013, 11:47:33 PM »
I am on part 2 of the tutorial and I am trying to wrap my head around list comprehensions. I understand most of it, what is stopping me up is this part:
Quote
One very important piece of advice: in list comprehensions, always call the constructor of the objects you're creating, like we did with Tile(False). If we had tried to first create an unblocked tile like floor = Tile(False) and then in the list comprehension just refer to that same floor, we'd get all sorts of weird bugs! This is a common rookie (and veteran!) mistake in Python. That's because all elements in the list would point to the exact same Tile (the one you defined as floor), not copies of it. Changing a property of one element would appear to change it in other elements as well! Calling the constructor for every element ensures that each is a distinct instance.

Can someone reword this for me? Here are the code snippets so no one has to go to the wiki:
Code: [Select]
class Tile:
    #a tile of the map and its properties
    def __init__(self, blocked, block_sight = None):
        self.blocked = blocked
 
        #by default, if a tile is blocked, it also blocks sight
        if block_sight is None: block_sight = blocked
        self.block_sight = block_sight

Code: [Select]
def make_map():
    global map
 
    #fill map with "unblocked" tiles
    map = [[ Tile(False)
        for y in range(MAP_HEIGHT) ]
            for x in range(MAP_WIDTH) ]

Thank you for your time.

Pages: [1]