# On using \`=\` vs \`:=\` for assignment

**URL:** <https://discourse.julialang.org/t/on-using-vs-for-assignment/110302>\
**Category:** Offtopic\
**Created:** [February 8, 2024, 3:46pm UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302 "2024-02-08T15:46:21Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 16, 2024, 5:04pm UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/21 "2024-02-16T17:04:30Z")

</div>

> [@mnemnion](#):
>
> Us mere users can’t gather up posts and bundle them into a new topic, so starting a different thread once it’s clear that we’ve drifted into some new interest means posting a new thread, posting a link to it in the old thread, and posting a link to the old thread so people can catch up on the conversation. This is an inferior version of what the moderators do anyway in breaking a thread off.

It’s a balancing act — the downside of having a moderator splitting off a thread is that the new thread can be difficult to read because it abruptly starts in the middle of a conversation. By starting a new thread intentionally, on the other hand, the initial poster can (hopefully) begin it with a clear summary of the new topic (even if it links to other topics).

See also this comment: [Time limits for unfocused discourse threads? - #11 by Nathan\_Boyer](https://discourse.julialang.org/t/time-limits-for-unfocused-discourse-threads/106739/11)

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [February 16, 2024, 7:06pm UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/22 "2024-02-16T19:06:23Z")

</div>

> **[Why Does "=" Mean Assignment?](https://www.hillelwayne.com/post/equals-as-assignment/)**
>
> Take the following code:
> a = 1 a = a + 1 print(a) A common FP critique of imperative programming goes like this: “How can a = a + 1? That’s like saying 1 = 2. Mutable assignment makes no sense.” This is a notation mismatch: “equals” should mean...

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 27, 2024, 2:46am UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/23 "2024-02-27T02:46:38Z")

</div>

I was rather enjoying the historical programming language musings. Julia does definitely inherit more from the Algol =\> Wirth lineage, mostly via Matlab, but not entirely. Turbo Pascal was also my first language (possibly also Jeff’s?) and I did a lot of Ruby in the 00’s.

I’m sympathetic to using `:=` for initial assignment / declaration and using `=` for reassignment, but I think that ship has sailed. Doing our own version of the [great vowel shift](https://en.wikipedia.org/wiki/Great_Vowel_Shift) and renaming `=` to `:=` and `==` to `=` and `===` to `==` might be better syntax if we could rewind history, but given the way languages have developed, that would be a very confusing choice.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [February 27, 2024, 6:52am UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/24 "2024-02-27T06:52:32Z")

</div>

While I concur that it is too late to change the meaning of `=` in Julia, it does seem that the walrus operator, `:=` has been reserved and thus could be defined in the future.

```julia-repl
julia> x := 5
ERROR: syntax: unsupported assignment operator ":="

```

There are a number of special assignment operations that I could think of.

1. Strict variable declaration. This would analogous to Go’s [short variable declaration](https://go.dev/tour/basics/10) syntax. In Julia’s case, this would ensure that we are assigning a new variable and not reassigning an old variable.
2. This could be also imply `local` variable assignment.

Consider the following idiosyncratic example.

```julia-repl
julia> let x = 5
           let
               x = 7
           end
           println(x)
       end
7

julia> let x = 5
           let
               local x = 7
           end
           println(x)
       end
5

```

We could have the walrus operator work as in the second example.

```julia-repl
julia> let x = 5
           let
               x := 7
           end
           println(x)
       end
5

```

1. We could use this for binding a type on assignment.

Similar to the following.

```julia-repl
julia> y::Float64 = 5.0
5.0

julia> y = 3 + 2im
ERROR: InexactError: Float64(3 + 2im)

```

We could do as follows.

```julia-repl
julia> y := 5.0
5.0

julia> y = 3 + 2im
ERROR: InexactError: Float64(3 + 2im)

```

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [February 27, 2024, 7:10am UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/25 "2024-02-27T07:10:37Z")

</div>

Imho `=` is ideal syntax for defining a variable, and defining rather than reassigning variables should be (slightly) encouraged. My preference is to make `:=` reassignment and keep `=` for defining. That’s the policy Guy Steele described for Fortress in his JuliaCon keynote.

In 1.x we can use a linter to make sure reassignment is always done with `:=`. Then in 2.0 we can make it the law.

> <https://github.com/JuliaLang/julia/issues/51223>
>
> This is inspired by https://github.com/JuliaLang/julia/issues/51107 and https://…github.com/JuliaLang/julia/issues/51183 where there is ambiguity between binding and assignment. I still dislike that when writing \`x =\` inside a function I have to worry about whether \`x\` is defined in the enclosing scope.
> 
> I like how Fortress distinguished assignment with \`:=\` from binding with \`=\` so it couldn't happen by accident (and as a one-character "tax" on assignment). In Scheme/Racket assignment is \`(set! x 2)\`. In Python you need \`nonlocal x\`. In Zig, shadowing is disallowed entirely. 
> 
> Suppose I want to always be explicit about whether I'm defining a new variable or changing the old one and have a linter disallow ambiguous cases. For defining a new variable there is \`local x\`. Could Julia also have a syntax for explicitly reassigning, like \`x := 2\` ?
> 
> Example
> 
> \`\`\`jl
> julia\> function f()
> x = 1
> function g()
> x = 2
> end
> g()
> x
> end
> f (generic function with 1 method)
> 
> julia\> f()
> 2
> \`\`\`

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 27, 2024, 10:37am UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/26 "2024-02-27T10:37:29Z")

</div>

> [@StefanKarpinski](#):
>
> I’m sympathetic to using `:=` for initial assignment / declaration and using `=` for reassignment, but I think that ship has sailed

I think that, for better or worse, a lot of the potential user base for Julia is used to the conventions of C, Fortran, and descendants (`=` for assignment, `==` for equality), so while such a notation would be consistent, it would have instantly alienated a large number of people.

This is not necessarily rational — personally, I think that I can get used to any kind of surface syntax (after all, I used Lisp a lot, so while I took the opportunity above to mention my Pascal-related traumas 😉, I could live with `:=`), and a large number of coherent and useful options are available for syntax. But it is one of those quirks of the human brain.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 27, 2024, 3:30pm UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/27 "2024-02-27T15:30:14Z")

</div>

This is the opposite meaning of Go, which strikes me as a rather bad choice. If we’re going to make the same distinction with the opposite meaning, that just seems willfully obtuse. Yes, Go is a different language, but languages are partially social constructs and you can’t just make up whatever notation you want (APL not withstanding); trying to avoid confusion based on what other languages have done matters. Using something like `x <- y` for rebinding would be better imo, or having an option to require `:=` for declaring assignment.

---

<div class="post-metadata">

**Author:** ![Janis\_Erdmanis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/janis_erdmanis/32/10869_2.png) [@Janis\_Erdmanis](https://discourse.julialang.org/u/Janis_Erdmanis)\
**Post date:** [February 27, 2024, 3:51pm UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/28 "2024-02-27T15:51:17Z")

</div>

I agree that reassignment needs to be more difficult, but I would not be willing to have `:=`. An alternative idea I have been sometimes dabbling with is that variables need explicitly be marked with `local` when reassignment is allowed:

```julia
function example()   
    local s = 0
    for i in 1:10
        s += i
    end
    return s
end

```

which would provide a warning or raise an error if `s` would not be marked as local.

---

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [February 27, 2024, 4:28pm UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/29 "2024-02-27T16:28:29Z")

</div>

> [@StefanKarpinski](#):
>
> I’m sympathetic to using `:=` for initial assignment / declaration and using `=` for reassignment, but I think that ship has sailed. Doing our own version of the [great vowel shift](https://en.wikipedia.org/wiki/Great_Vowel_Shift) and renaming `=` to `:=` and `==` to `=` and `===` to `==` might be better syntax if we could rewind history, but given the way languages have developed, that would be a very confusing choice.

It’s clear that “`=` means equality in mathematics, let’s change 6/10 lines of code written in Julia and make everyone’s life harder thereby” is a non-starter, yes.

But I could see “`:=` means declaration” being a win for 2.0. We’ve seen a few variations on that already, so I’ll sketch out mine.

I like the scoping rules in Lua, but I don’t like that `local` is five frikkin letters long and pushes declarations off to the right. It’s a very heavy word for what it does! Julia has the same keyword with the ~same meaning, but it doesn’t matter because the scope rules are different, it won’t silently dump your variable in the global environment table if you don’t tell it not to.

With `:=` a 2.0 could move toward Lua’s scope rules with some ancillary benefits. Lua has a very simple rule that a block is a scope, I prefer the Julian distinction between scope-introducing blocks and scopeless blocks (let/for vs. if/begin).

But if in 2.0 a variable declaration required `:=`, Julia wouldn’t need `local` or `global`. It would still need `outer` however. This would create less of a hard line between top scope and inner scopes, which I consider a good 2.0 goal, along with eliminating syntax which is only legal in inner scopes, or at least reducing it to a bare minimum (macro declarations might have to stay global, for example).

It seems a bit redundant to require it in `let` expressions, which introduce a declaration without exception, so let’s say I’m agnostic as to whether it should be required there (it would need to be permitted at least).

Julia does a rather good job with handling implicit declaration relative to other languages (Python) which use it, but there’s an inherent conceptual ambiguity which requires closer attention to the scope rules to write correct code. Using a dedicated syntax for declaration minimizes that ambiguity, if not eliminates it.

It’s also nice that it would make repeated definitions in a single scope an error. There are two contexts where that would make a difference: global scope, which can be scattered across thousands of lines in many files, and complex `if` statements, where one might think that every declaration is mutually exclusive, but the compiler might disagree.

It could be introduced as an optional syntax in 1.0, since it’s a syntax error now. Then `=` for declaration could be deprecated, with a rewrite tool which translates code to the new standard: the compiler knows which is which, after all, so this isn’t a guessing game. Then 2.0 could complete the transition, and change the scope rules, so `() -> (b = 5; return b)` either refers to an outer scope variable (up to global) or is an error.

That would also buy some time to see if users hate it. I don’t think they would, but you never know.

I’m not even convinced it would be worth the hassle, but I do know that I’d be pleased with the change.

> [@Tamas\_Papp](#):
>
> I think that, for better or worse, a lot of the potential user base for Julia is used to the conventions of C, Fortran, and descendants (`=` for assignment, `==` for equality), so while such a notation would be consistent, it would have instantly alienated a large number of people.

It’s worth noting that Go has a much larger user base than Julia (for now) and uses `:=` in much the way we’re discussing. This gets dissed on Hacker News sometimes but clearly poses no barrier to adoption.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [February 27, 2024, 7:26pm UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/30 "2024-02-27T19:26:20Z")

</div>

The reversal from Go is a definite negative. That said, only 3% of Julia users use Go ([2023 Julia survey](https://julialang.org/assets/2023-julia-user-developer-survey.pdf)) so I suspect it’s relatively unlikely to cause excessive confusion in practice.

In other languages there are different uses too. Eg 14% of Julians use SQL which uses `=` for equality testing. 6% use Mathematica in which [`lhs = rhs`](https://reference.wolfram.com/language/ref/Set.html) is “immediate assignment, with rhs evaluated at the time of assignment” and [`lhs := rhs`](https://reference.wolfram.com/language/ref/SetDelayed.html) is “delayed assignment, with rhs reevaluated every time it is used”.

---

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [February 27, 2024, 8:05pm UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/31 "2024-02-27T20:05:56Z")

</div>

> [@jar1](#):
>
> The reversal from Go is a definite negative. That said, only 3% of Julia users use Go ([2023 Julia survey](https://julialang.org/assets/2023-julia-user-developer-survey.pdf)) so I suspect it’s relatively unlikely to cause excessive confusion in practice.

The set of Julia users is a tiny fraction of the set of potential Julia users, and (I hope at least) also a small fraction of eventual Julia users. Paying attention to how larger languages use syntax should be a consideration.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [February 27, 2024, 8:33pm UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/32 "2024-02-27T20:33:19Z")

</div>

there could potentially be an interesting way to use `:` as an operator; if so then `:=` probably should not be used except to mean `op=`

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 27, 2024, 8:43pm UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/33 "2024-02-27T20:43:16Z")

</div>

> [@StefanKarpinski](#):
>
> I’m sympathetic to using `:=` for initial assignment / declaration and using `=` for reassignment

This kind of dichotomy would be a huge pain for interactive use — if you have a long-running interactive session, every time you assigned a variable you would have to remember whether you had used that variable name before.

> [@StefanKarpinski](#):
>
> but I think that ship has sailed.

Yes, I think [PSA: Julia is not at that stage of development anymore](https://discourse.julialang.org/t/psa-julia-is-not-at-that-stage-of-development-anymore/44872) needs to be re-iterated here. It’s fun to talk about the etymology of syntaxes and what-might-have-beens, but no one is seriously contemplating major changes to Julia’s spelling/aesthetics at this point. If people think they are arguing over the future direction of Julia in this thread, they are bound to be disappointed.

---

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [February 27, 2024, 9:01pm UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/34 "2024-02-27T21:01:34Z")

</div>

> [@stevengj](#):
>
> It would be a huge pain for interactive use — if you have a long-running interactive session, every time you assigned a variable you would have to remember whether you had used that variable name before.

The REPL already has a few differences from scripts, and any hypothetical `:=` for declaration could simply be relaxed in that context.

> [@stevengj](#):
>
> Yes, I think [PSA: Julia is not at that stage of development anymore](https://discourse.julialang.org/t/psa-julia-is-not-at-that-stage-of-development-anymore/44872) needs to be re-iterated here. It’s fun to talk about the etymology of syntaxes and what-might-have-beens, but no one is seriously contemplating major changes to Julia’s spelling/aesthetics at this point. If people think they are arguing over the future direction of Julia in this thread, they are bound to be disappointed.

Just idle chitchat as far as I’m concerned. I probably see the suggestion as less major, and less aesthetically-grounded, than you do, but no matter. Work on a 2.0 hasn’t begun, and it’s not clear that it ever will, so it seems like a safe place to store alternate-universe Julia fanfic. IMHO the only reason to introduce an optional declaration syntax would be as a transition to a 2.0 where it was load-bearing, the language doesn’t need more ways to spell the same thing.

I think my proposal would be a concrete improvement, not window-dressing, or I wouldn’t have bothered making it. But (as I believe I indicated all the way at the top) I’d expect to get voted down because people don’t like how it looks. Which is a valid reason, I don’t like working with languages where I have aesthetic objections to the syntax either. Of which I have none at all for Julia, which is unusual though not quite unique.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [February 27, 2024, 9:43pm UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/35 "2024-02-27T21:43:13Z")

</div>

For me, accidental reassignment is definitely frequent and consequential enough to merit significant syntax changes. To wit:

`=` defines a local variable.

```julia
julia> x = 1
1

julia> let
           x = 2
       end
2

julia> x
1

```

So far so good.

```julia
julia> let x = 1
           let x = 2
           end
       x
       end
1

```

Cool, all good.

So then when it bites, it’s actually much more surprising:

```julia
julia> let x = 1
           function f(y)
               x = abs(y)
               y - x
           end
           sort(1:5; by=f)
           x
       end
4

```

Whenever I define a variable in a block, I try to read every line lexically above it to make sure the variable doesn’t exist already, lest I corrupt my runtime state by accident. Variable definition is therefore a quadradic-time operation, which is quite annoying.

Worse, I still sometimes fail to notice `x` already exists and get confused why variables are different than I left them. I hope my testing is always good enough to catch these mistakes, but I don’t want to rely on that: I want a guarantee that when I define `x = 2` all the information I need is right there on the line I’m currently looking at.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [February 27, 2024, 9:52pm UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/36 "2024-02-27T21:52:45Z")

</div>

> [@jar1](#):
>
> Cool. So when it bites

is that really a “bite” ? isn’t this a direct consequence of like scoping rule #1 ? I don’t mean to sound condescending, only that I feel you are presenting this as a ‘WAT’ when I see it as just very fundamental to how Julia scope rules work

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [February 27, 2024, 10:00pm UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/37 "2024-02-27T22:00:12Z")

</div>

I do get the sense that a similar PSA is needed regarding what “Julia 2” might be. If we have learned anything ftom the experience of other languages, most but perhaps not necessarily all “Julia 1” code should continue to work under “Julia 2”.

Changing the meaning of `=` or requiring the use of `:=` does not seem compatible with the idea of “Julia 1” code continuing to work under “Julia 2”.

---

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [February 27, 2024, 10:08pm UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/38 "2024-02-27T22:08:04Z")

</div>

> [@mkitti](#):
>
> I do get the sense that a similar PSA is needed regarding what “Julia 2” might be. If we have learned anything from the experience of other languages, most but perhaps not necessarily all “Julia 1” code should continue to work under “Julia 2”.

This seems entirely premature if there’s no Julia 2 on the horizon. If there are official statements on the subject, that should be downstream from an solid intention to write it.

> [@mkitti](#):
>
> Changing the meaning of `=` or requiring the use of `:=` does not seem compatible with the idea of “Julia 1” code continuing to work under “Julia 2”.

I wouldn’t conflate these two things. Switching `=` to `:=` and `==` to `=` is purely cosmetic and worth rejecting out of hand.

There are a few strategies to achieve a laudable goal like most Julia 1 code continuing to work under Julia 2. In the specific case of `:=` for declaration, that’s a translation a program can make with perfect fidelity, either at load time, by changing the file, or both.

It should in fact be possible, with the right process, to assure that 100% of Julia 1 code can be executed in the Julia 2 runtime. But a detailed exploration of that thesis would be off topic.

There are plenty of good arguments against `:=` for declaration, but ruling it out on procedural grounds is suboptimal.

> [@jar1](#):
>
> Worse, I still sometimes fail to notice `x` already exists and get confused why variables are different than I left them. I hope my testing is always good enough to catch these mistakes, but I don’t want to rely on that: I want a guarantee that when I define `x = 2` all the information I need is right there on the line I’m currently looking at.

I’ve written a bug the other way, where I had a global variable I was trying to close over and neglected the fact that global scope is “special”. So `:=` works both ways there.

> [@adienes](#):
>
> is that really a “bite” ? isn’t this a direct consequence of like scoping rule #1 ? I don’t mean to sound condescending, only that I feel you are presenting this as a ‘WAT’ when I see it as just very fundamental to how Julia scope rules work

Python’s scoping rules are clearly defined, but complex, in a way that leads to authors, especially beginners, making frequent mistakes. Julia’s are better, but this doesn’t mean they can admit of no improvement.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [February 27, 2024, 10:39pm UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/39 "2024-02-27T22:39:56Z")

</div>

> [@mnemnion](#):
>
> Python’s scoping rules are clearly defined, but complex, in a way that leads to authors, especially beginners, making frequent mistakes. Julia’s are better

The mistake in my demo above does not occur in Python. It would require `nonlocal x` in order to reassign nonlocal variables.

Nor does it occur in Scheme which uses `set!` for assignment, nor Rust which uses `let` for definition, nor Go which distinguishes `=` from `:=`, nor Zig, etc.

---

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [February 28, 2024, 12:00am UTC](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302/40 "2024-02-28T00:00:56Z")

</div>

> [@jar1](#):
>
> The mistake in my demo above does not occur in Python. It would require `nonlocal x` in order to reassign nonlocal variables.

In Python it’s the other version of the problem, where you can see a variable from an enclosing scope on the right-hand side of a statement, but using it as an lvalue will create a new variable instead of assigning to the outer one. This issue was particularly acute before `nonlocal` came along in Python 3, because you just couldn’t do it. Julia does this for globals, but since module-level names can be declared anywhere in the module, I think it’s the better choice there, instead of switching the meaning of a variable in a function you weren’t thinking about when you add a global, possibly in a different file.

We agree that a lightweight syntax to distinguish declaration and assignment is preferable to learning the ins and outs of implicit scope rules and applying modifiers to get exceptions to the rule. None of these issues would come up and neither `global` nor `local` would be necessary.

[Previous page](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302.md?page=1)

[Next page](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302.md?page=3)
