c# - Python global variable referenced before assigned a value -


i started program in python, , love far. programmed in c# , java, causing problem. in c#, if have public variable, change in each method. sorry bad explanation, easier visualize code.

this code example of want happen in python in c# form. code not work because it's example.

class player {     public var player; //create variable     public var playerrectangle;     public var playermovement;      public player()     {         player = pygame.image.load("player.png"); //set variables value         playerrectangle = player.get_rect();          playermovement = new int[0,0];     }      public void update()     {         event in pygame.event.get():             if event.type == pygame.keydown , event.key == pygame.k_w:                 playermovement[1] = -2   //use variables value             if event.type == pygame.keydown , event.key == pygame.k_s:                 playermovement[1] = 2             if event.type == pygame.keydown , event.key == pygame.k_a:                 playermovement[0] = -2             if event.type == pygame.keydown , event.key == pygame.k_d:                 playermovement[0] = 2              if event.type == pygame.keyup , event.key == pygame.k_w:                 playermovement[1] = 0             if event.type == pygame.keyup , event.key == pygame.k_s:                 playermovement[1] = 0             if event.type == pygame.keyup , event.key == pygame.k_a:                 playermovement[0] = 0             if event.type == pygame.keyup , event.key == pygame.k_d:                 playermovement[0] = 0          playerrectangle = playerrectangle.move(playermovement)     } }  

the actual python code is:

import pygame  class player:     player = none #create variable     playerrectangle = none     playermovement = none      def __init__():         global player         player = pygame.image.load("player.png") #set variables value         global playerrectangle         playerrectangle = player.get_rect()          global playermovement         playermovement = [0,0]      def update():         event in pygame.event.get():             if event.type == pygame.keydown , event.key == pygame.k_w:                 playermovement[1] = -2 #use variables !!error!!             if event.type == pygame.keydown , event.key == pygame.k_s:                 playermovement[1] = 2             if event.type == pygame.keydown , event.key == pygame.k_a:                 playermovement[0] = -2             if event.type == pygame.keydown , event.key == pygame.k_d:                 playermovement[0] = 2              if event.type == pygame.keyup , event.key == pygame.k_w:                 playermovement[1] = 0             if event.type == pygame.keyup , event.key == pygame.k_s:                 playermovement[1] = 0             if event.type == pygame.keyup , event.key == pygame.k_a:                 playermovement[0] = 0             if event.type == pygame.keyup , event.key == pygame.k_d:                 playermovement[0] = 0          playerrectangle = playerrectangle.move(playermovement)      def returntexture():         return player      def returnrectangle():         return playerrectangle 

it says variable referenced before given value, gave value in constructor.

you have forgotten (or have not read) use of self in methods.

you need put self in method definitions , use self.<attribute> reference attributes on 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: use of self in method definitions of classes in python convention only; can use whatever name , typically (by convention) use self.

see: classes

see also: what purpose of self?

side note: 1 of reasons (btw) in python more explicit how write code. see 2nd line of zen of python

the zen of python

beautiful better ugly.  explicit better implicit.  ... 

update: it's worth nothing (please read documentation) there no notion of public vs. private members python's type system. define "private" members convention prefixed single underscore; e.g: def _foo(self): private/inernal method.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -