Skip to main content
added 40 characters in body
Source Link
candied_orange
  • 114.6k
  • 27
  • 222
  • 352

x = 10
x = 20

x = 10  
x = 20

This is not shadowing.

This is shadowing:

x = 0

def outer():
    x = 1

    def inner():
        x = 2
        print("inner:", x)

    inner()
    print("outer:", x)

outer()
print("global:", x)

# prints
# inner: 2
# outer: 1
# global: 0

Each x belongs to a different scope. So they lead completely different lives. You just can't use them all at the same time because they get in each others way. Ya know, casting shadows on each other.

x = 10
x = 20

x = 10  
x = 20

Same scope, same x. With this code that 10 is gone. You ain't getting it back. The question is, why?

  • Rebinding is a change to the referencing identifier.
  • Assignment is a change to (the referenced) variable.
  • Mutation is a change to an object in memory, possibly referenced by a variable or bound to an identifier.

It depends on how your language implements that code.

  • Rebinding -> The 10 is lost. It exists somewhere but we forgot where. But we know where this nifty 20 is.

  • Assignment -> The 10 is dead. It now looks like a 20.

  • Mutation -> Can happen to values and to references. Which happened here depends on your language.

x = 10
x = 20

This is not shadowing.

This is shadowing:

x = 0

def outer():
    x = 1

    def inner():
        x = 2
        print("inner:", x)

    inner()
    print("outer:", x)

outer()
print("global:", x)

# prints
# inner: 2
# outer: 1
# global: 0

Each x belongs to a different scope. So they lead completely different lives. You just can't use them all at the same time because they get in each others way. Ya know, casting shadows on each other.

x = 10
x = 20

With this code that 10 is gone. You ain't getting it back. The question is, why?

  • Rebinding is a change to the referencing identifier.
  • Assignment is a change to (the referenced) variable.
  • Mutation is a change to an object in memory, possibly referenced by a variable or bound to an identifier.

It depends on how your language implements that code.

  • Rebinding -> The 10 is lost. It exists somewhere but we forgot where. But we know where this nifty 20 is.

  • Assignment -> The 10 is dead. It now looks like a 20.

  • Mutation -> Can happen to values and to references. Which happened here depends on your language.

x = 10  
x = 20

This is not shadowing.

This is shadowing:

x = 0

def outer():
    x = 1

    def inner():
        x = 2
        print("inner:", x)

    inner()
    print("outer:", x)

outer()
print("global:", x)

# prints
# inner: 2
# outer: 1
# global: 0

Each x belongs to a different scope. So they lead completely different lives. You just can't use them all at the same time because they get in each others way. Ya know, casting shadows on each other.

x = 10  
x = 20

Same scope, same x. With this code that 10 is gone. You ain't getting it back. The question is, why?

  • Rebinding is a change to the referencing identifier.
  • Assignment is a change to (the referenced) variable.
  • Mutation is a change to an object in memory, possibly referenced by a variable or bound to an identifier.

It depends on how your language implements that code.

  • Rebinding -> The 10 is lost. It exists somewhere but we forgot where. But we know where this nifty 20 is.

  • Assignment -> The 10 is dead. It now looks like a 20.

  • Mutation -> Can happen to values and to references. Which happened here depends on your language.

Source Link
candied_orange
  • 114.6k
  • 27
  • 222
  • 352

x = 10
x = 20

This is not shadowing.

This is shadowing:

x = 0

def outer():
    x = 1

    def inner():
        x = 2
        print("inner:", x)

    inner()
    print("outer:", x)

outer()
print("global:", x)

# prints
# inner: 2
# outer: 1
# global: 0

Each x belongs to a different scope. So they lead completely different lives. You just can't use them all at the same time because they get in each others way. Ya know, casting shadows on each other.

x = 10
x = 20

With this code that 10 is gone. You ain't getting it back. The question is, why?

  • Rebinding is a change to the referencing identifier.
  • Assignment is a change to (the referenced) variable.
  • Mutation is a change to an object in memory, possibly referenced by a variable or bound to an identifier.

It depends on how your language implements that code.

  • Rebinding -> The 10 is lost. It exists somewhere but we forgot where. But we know where this nifty 20 is.

  • Assignment -> The 10 is dead. It now looks like a 20.

  • Mutation -> Can happen to values and to references. Which happened here depends on your language.