kommihe
kommihe

Reputation: 571

is the variable passed as a pointer for some reason?

The code I made works, but I want to know why. I accidentally made a mistake in the code, but for some reason it works, why? When I pass lastkeys to move.mov, shouldn't keys be a new variable? The same with ent.playerlocation, shouldn't be player in move.mov be a new variable? The original variable is changed to the value of the new one when I exit the function. I've tried to recreate this, but haven't been able to.

main.py:

import pygame
import move, updatescreen

class entities:
    def __init__(self):
        self.playerlocation = [64,64]

if __name__ == '__main__':
    pygame.init()
    screen=pygame.display.set_mode((640,360),pygame.RESIZABLE,32)#pygame.FULLSCREEN
    pygame.display.set_caption('Pygame Window')

    ent = entities()
    lastkeys = [0,0,0,0]
    ispaused = 0

    while True:
        move.userevents(lastkeys)
        move.mov(lastkeys, ent.playerlocation)
        updatescreen.gameupdate(screen,ent)

move.py:

import pygame, sys

def userevents(keys):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                keys[0] = 1
            elif event.key == pygame.K_RIGHT:
                keys[1] = 1
            elif event.key == pygame.K_UP:
                keys[2] = 1
            elif event.key == pygame.K_DOWN:
                keys[3] = 1
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                keys[0] = 0
            elif event.key == pygame.K_RIGHT:
                keys[1] = 0
            elif event.key == pygame.K_UP:
                keys[2] = 0
            elif event.key == pygame.K_DOWN:
                keys[3] = 0

def mov(keys,player):
    if keys[0]:
        player[0] -=1
    elif keys[1]:
        player[0] +=1
    if keys[2]:
        player[1] -=1
    elif keys[3]:
        player[1] +=1

Upvotes: 0

Views: 148

Answers (2)

shu zOMG chen
shu zOMG chen

Reputation: 436

When you pass in a mutable object it gets passed in by reference (thus, any modifications you make to it affects the original object). For example:

>>> def change_list(lst):
...   lst[0] = lst[0] * 2
... 
>>> a = [1,2,3]
>>> change_list(a)
>>> a
[2, 2, 3]

So either make a copy of the list before you pass it into the function:

>>> def change_list(lst):
...   lst[0] = lst[0] * 2
... 
>>> a = [1,2,3]
>>> cpy_a = list(a)
>>> change_list(cpy_a)
>>> a
[1, 2, 3]

Or after it's passed into the function:

>>> def change_list(lst):
...   lst_cpy = list(lst)
...   lst_cpy[0] = lst_cpy[0] * 2
... 
>>> a = [1,2,3]
>>> change_list(a)
>>> a
[1, 2, 3]

For a more thorough discussion, please see: How do I pass a variable by reference?

Upvotes: 2

Ferdinand Beyer
Ferdinand Beyer

Reputation: 67177

When I pass lastkeys to move.mov, shouldn't keys be a new variable?

No, objects are always passed by reference in Python. If you pass a list to a function, and the function modifies the list, then the modification is visible from the caller side afterwards.

Upvotes: 1

Related Questions