15 ms·
Manim is incredibly cool, but my biggest takeaway from the video is how insane it is that Python apparently lets you reference a variable in a function that was
by chris12321 2y ago
Manim is incredibly cool, but my biggest takeaway from the video is how insane it is that Python apparently lets you reference a variable in a function that was defined outside the scope of the function.
- Rygian 2y agoIt's a whole subject in language design: https://en.wikipedia.org/wiki/Scope_(computer_science) https://en.wikipedia.org/wiki/Scope_(computer_science)
- epistasis 2y agoMaybe I'm misunderstanding you, but isn't that standard for pretty much every C-style language? Define a variable outside a function, parallel to the function, and even though that variable is outside the scope of the function it's still accessible inside the function. Or did you mean something else?
- rzzzt 2y agoPython has nested functions which does allow access to the nest...er function's variables. The rest does conform to expectations, you get to use parameters, locally declared variables and globals.
- antonvs 2y agoI'm not too familiar with Python, but isn't that just lexical scope, the same as most languages that support nested functions? Starting with the lambda calculus and all the languages it influenced, including even JavaScript.
- meowface 2y agoCorrect. It behaves like JavaScript and many other languages. You can't modify such variables in the outer scope from the inner scope unless you explicitly use the "nonlocal" declaration.
- chris12321 2y agoIn languages I'm most familiar with, you can't access variables defined outside of the function within the function unless the variable is a class or global variable. So for example in Python you can do: >>> x = "hello" >>> def test(): ... print(x) ... >>> test() hello which seems very odd to me. In Ruby you get: irb(main):001> x = "hello" => "hello" irb(main):002\* def test irb(main):003\* puts x irb(main):004> end => :test irb(main):005> test (irb):3:in `test': undefined local variable or method `x' for main:Object (NameError) This makes much more sense to me, since x is defined outside of the scope of test, so why should test have access to it?