3 ms·
Python Scope and the LEGB Rule: Resolving Names in Your Code
- eesmith 7y agoThere is one more scope in Python - stack scope. It's possible to peer up the call stack to get information from your callers, rather than static scope. There's also the oddity that is super(): class Base: def foo(self): print("foo") def bar(self): print("bar") def baz(self): print("baz") class Derived(Base): def foo(self): print("Derived1"); foo_super = super foo_super().foo() def bar(self): bar_super = super def bar_func(): print("Derived2"); bar_super().bar() bar_func() def baz(self): print("Derived3") baz_super().baz() baz_super = super >>> x = Derived() >>> x.foo() Derived1 foo >>> x.bar() Derived2 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 12, in bar File "<stdin>", line 11, in bar_func RuntimeError: super(): no arguments >>> x.baz() Derived3 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 16, in baz RuntimeError: super(): __class__ cell not found