Python global variable referenced before assigned a value
Question: I recently started to program in python, and I love it so far. I previously
programmed in c# and java, which is probably causing my problem. In c#, if you
have a public variable, it will change in each method. Sorry for the bad
explanation, but it will be easier to visualize the code.

This code is an example of what I want to happen in python in c# form. The
code does not actually work because it's only an example.

    
    
    class Player
    {
        public var player; //create the variable
        public var playerRectangle;
        public var playerMovement;
    
        public Player()
        {
            player = pygame.image.load("player.png"); //set the variables value
            playerRectangle = player.get_rect();
    
            playerMovement = new int[0,0];
        }
    
        public void Update()
        {
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN and event.key == pygame.K_w:
                    playerMovement[1] = -2   //use the variables value
                if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
                    playerMovement[1] = 2
                if event.type == pygame.KEYDOWN and event.key == pygame.K_a:
                    playerMovement[0] = -2
                if event.type == pygame.KEYDOWN and event.key == pygame.K_d:
                    playerMovement[0] = 2
    
                if event.type == pygame.KEYUP and event.key == pygame.K_w:
                    playerMovement[1] = 0
                if event.type == pygame.KEYUP and event.key == pygame.K_s:
                    playerMovement[1] = 0
                if event.type == pygame.KEYUP and event.key == pygame.K_a:
                    playerMovement[0] = 0
                if event.type == pygame.KEYUP and event.key == pygame.K_d:
                    playerMovement[0] = 0
    
            playerRectangle = playerRectangle.move(playerMovement)
        }
    } 
    

The actual python code is:

    
    
    import pygame
    
    class Player:
        player = None #create the variable
        playerRectangle = None
        playerMovement = None
    
        def __init__():
            global player
            player = pygame.image.load("player.png") #set the variables value
            global playerRectangle
            playerRectangle = player.get_rect()
    
            global playerMovement
            playerMovement = [0,0]
    
        def update():
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN and event.key == pygame.K_w:
                    playerMovement[1] = -2 #use the variables !!ERROR!!
                if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
                    playerMovement[1] = 2
                if event.type == pygame.KEYDOWN and event.key == pygame.K_a:
                    playerMovement[0] = -2
                if event.type == pygame.KEYDOWN and event.key == pygame.K_d:
                    playerMovement[0] = 2
    
                if event.type == pygame.KEYUP and event.key == pygame.K_w:
                    playerMovement[1] = 0
                if event.type == pygame.KEYUP and event.key == pygame.K_s:
                    playerMovement[1] = 0
                if event.type == pygame.KEYUP and event.key == pygame.K_a:
                    playerMovement[0] = 0
                if event.type == pygame.KEYUP and event.key == pygame.K_d:
                    playerMovement[0] = 0
    
            playerRectangle = playerRectangle.move(playerMovement)
    
        def returnTexture():
            return player
    
        def returnRectangle():
            return playerRectangle
    

It says that the variable is referenced before it was given a value, but I
gave it a value in the constructor.


Answer: You have forgotten (_or have not read_) the use of `self` in your methods.

You need to put `self` in the method definitions and use `self.<attribute>` to
reference attributes on the instance.

**Example:**

    
    
    class Player(object):
    
        def __init__(self, name):
            self.name = name
    
        def update_name(self, name):
            self.name = name
    
    bob = Player("Bob")
    print bob.name  # Bob
    
    bob.update_name("Bob Jones")
    print bob.name  # Bob Jones
    

**Note:** That the use of `self` in method definitions of classes in Python is
by convention only; you can use whatever name you like but we widely and
typically (_by convention_) use `self`.

See: [Classes](https://docs.python.org/2/tutorial/classes.html)

See Also: [What is the purpose of self in
Python?](http://stackoverflow.com/questions/2709821/what-is-the-purpose-of-
self-in-python)

**Side Note:** One of the reasons for this (_btw_) is that in Python we like
to be more explicit about how we write code. See the 2nd line of the [Zen of
Python](https://www.python.org/dev/peps/pep-0020/)

> The [Zen of Python](https://www.python.org/dev/peps/pep-0020/)
>  
>  
>     Beautiful is better than ugly.
>  
>     Explicit is better than implicit.
>  
>     ...
>  

**Update:** It's also worth nothing (_please read the documentation_) that
there is no notion of public vs. private members with Python's type system. We
define "private" members by convention only usually with a prefixed single
underscore; e.g: `def _foo(self):` as a private/inernal method.

