r/AskComputerScience • u/myyylu • May 22 '26
What is the difference between binding and scope in programming languages?
It might be a stupid question but I've been having such a hard time understanding what the difference bewteen these two is, because every time I think I understand it I realize I'm either still confused or I'm still thinking of them as the same thing. Especially once dynamic and static binding and scope enter the discussion.
6
Upvotes
7
u/ghjm MSCS, CS Pro (20+) May 22 '26
They are strongly related concepts. Binding means assigning a name to something. Scope means defining the context in which that name is meaningful.
Specifically, during compilation, each node in the AST has an attached list of zero or more names and what they mean. A binding is the data element that gets added to these lists. A scope is which node this binding is attached to, along with some rule like "if you don't find a name in your local node, recursively look at its parents."
1
u/alecbz May 22 '26
Scope is much more "local" and something that's often evident from just reading an individual piece of code. It has to do with how long a given local variables "lives", what sections of the code are allowed to refer to it.
E.g., in python if you define a variable at the beginning of a function, its scope is that function. You can refer to that variable anywhere within that function's body, but not in other functions or in global code, where it's "out of scope"
Dynamic binding is pretty different, it's the idea (usually in more statically typed languages like Java and C++) that something like
foo.String()does not call one single "String" method, instead we look up the.String()method that's relevant to the actual concrete typefoois and then we call into that specific implementation. It's notable because we can't know what.String()to call until runtime, where we know whatfoo's concrete type will actually be.8
u/[deleted] May 22 '26
[removed] — view removed comment