News for hackers. Stuff that matters.
Re:How I Animate 3Blue1Brown by chris12321 on Saturday October 12, 2024 @04:40PM (#41822367)
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.
Re:How I Animate 3Blue1Brown by epistasis on Saturday October 12, 2024 @04:44PM (#41822398)
Maybe 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?
Re:How I Animate 3Blue1Brown by rzzzt on Saturday October 12, 2024 @04:53PM (#41822463)
Python 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.
Re:How I Animate 3Blue1Brown by antonvs on Saturday October 12, 2024 @08:02PM (#41823910)
I'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.
Re:How I Animate 3Blue1Brown by meowface on Saturday October 12, 2024 @09:27PM (#41824385)
Correct. 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.
Re:How I Animate 3Blue1Brown by chris12321 on Saturday October 19, 2024 @10:10AM (#41887853)
In 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?
Re:How I Animate 3Blue1Brown by Rygian on Saturday October 12, 2024 @04:43PM (#41822387)
It's a whole subject in language design: https://en.wikipedia.org/wiki/Scope_(computer_science)