variable assignment error, reference before asignment?

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 449 Views - Last Post: 09 September 2013 - 08:37 PM Rate Topic: -----

#16 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,749
  • Joined: 19-March 11

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 08:15 PM

Walk through this and understand it. Experiment in the repl until it makes sense.


#!/usr/bin/python

x = 0    # x is a global

y = x+5  # y is a global defined in terms of x

def foo (x) :  # foo is a function which takes a single value as a parameter, and it calls that value "x"
  global y
  print x   
  y = x + 23
  z = 3 * x
  return y + z

print x      # prints 0 ---  as defined
print y      # prints 5 ---  as defined
q = foo (2)  # prints 2, as side effect - the "x" in foo is local and not related to the x we defined at global scope
print x      # prints 0 ---- again, x in foo was local, and we've forgotten all about that one now
print y      # prints 25 - y was declared global, so the change to y was "for realsies"
print (q)    # prints (2 + 23) + (3 * 2)
print z      # fails, z does not exist

Was This Post Helpful? 1
  • +
  • -

#17 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 08:17 PM

You can do it either way.

var1 = 0

def function(num):
    temp = num + 5
    return temp

var1 = function(var1)



var1 = 0

def function():
    global var1
    var1 += 5

function(var1)



The above two pieces of code are functionally similar, but with one major difference: the second block is locked into utilizing var1 regardless of what you actually want to do. The first block is then more flexible because you can have it perform its function on whatever number you choose to pass to it. In the second block, you don't have the option to change it from using var1 without rewriting it, and it is less scalable because "violations" of scope like this become difficult to follow in larger codebases.

EDIT: jon.kiparsky's example is much better. Definitely read through it and see how it works! It's getting quite late for me :)

This post has been edited by Valek: 09 September 2013 - 08:20 PM

Was This Post Helpful? 0
  • +
  • -

#18 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,749
  • Joined: 19-March 11

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 08:26 PM

View Postrippinrob, on 09 September 2013 - 10:09 PM, said:

By looking at the code, im assuming the y and x variables in my ifs are referring to the ones stated as global x,y in my function... and since i declared them global, they look for and refer to global variables declared above all functions.

so there is no point to declare global variables, that are outside of all the functions... because they ARE global... only for ones inside functions to point BACK to ones i declared outside of them...

am i right? close?

and I bookmarked the read you linked, it looks to have some laymen terms.... having to go to bed soon for school tomorrow, I will read it tomorrow.




There are basically three ways to maintain state. You can use globals, or you can pass state around, or you can use objects. The classic functional approach to state is to do something like

position = update_position(position, delta)


def update_position(current_pos, delta):
  return (current_pos[0] + delta [0], current_pos[1] + delta [1])




That is, you start with a position, and you apply some change to it, and you record the result.

The classic procedural approach is to have a global variable somewhere:

update_position(delta)


def update_position(delta):
  global current_position
  current_position = (current_pos[0] + delta [0], current_pos[1] + delta [1])



In this case, there's a tuple somewhere out in the world, and we're modifying it.


The classic object-oriented paradigm looks like this:


thing.update_position(delta)


class Thing:
  def __init__(self):
    self.position = (0,0)


  def update_position(self, delta):
    self.position = (current_pos[0] + delta [0], current_pos[1] + delta [1])




Now here we have a thing, and the thing knows where it is and how to change its location.



Python is happy to do any of these things for you, they all work just fine. What doesn't work is this:


update_position(position, delta)


def update_position(current_pos, delta):
  current_pos = current_pos[0] + delta [0], current_pos[1] + delta [1])




The parameter list of a method is really a set of local variable declarations. So what's happening here is, you're taking in two parameters, assigning them to local variables, modifying one of the local variables, and then discarding both of them.
Was This Post Helpful? 1
  • +
  • -

#19 rippinrob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-August 13

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 08:37 PM

Thanks everyone for the help! im getting a much better understanding of scopes. Seeing it as a state is good.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2