Python Forum
i guess i don't really understand locals
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i guess i don't really understand locals
#1
def funa():
    print('started funa')
    foo = 'bar'
    def funb():
        print('started funb')
        print('locals() =',repr(locals()))
        print('locals() has "foo":','foo' in locals())
        print('end funb')
    def func():
        print('started func')
        print('locals() =',repr(locals()))
        print('locals() has "foo":','foo' in locals())
        print('foo =',repr(foo))
        print('locals() =',repr(locals()))
        print('locals() has "foo":','foo' in locals())
        print('end func')
        return
    funb()
    func()
    print('end funa')
    return
funa()
then when i run it i get:
Output:
started funa started funb locals() = {} locals() has "foo": False end funb started func locals() = {'foo': 'bar'} locals() has "foo": True foo = 'bar' locals() = {'foo': 'bar'} locals() has "foo": True end func end funa
when funb() or func() is called, they may or may not access the callers locals. this seems to depend on the compiler seeing it or something. this could break accessing locals()[name] where name refers to "foo". i think i need to play with it more to see what it really does.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020