Python's Globals () and locals ()

Source: Internet
Author: User

Global () Returns the property dictionary for the global scope, locals () returns the property dictionary for the local scope. The value returned when locals () is positioned at the top level of the module is the same as global ().

Instance

#!/usr/bin/python#coding =utf-8 class Dog(object):x = Oney = A     def __init__(self):m = -n = -     def eat(self):K = Oneh = A        ' print {' H ': ' K ': One, ' self ': <__main__. Dog object at 0x7fc2985b8e10>} "        PrintLocals () def func():x =1y =2     def wrap():        Pass    "Print Func Local is: {' wrap ': <function wrap at 0x7fc2985be938>, ' Y ': 2, ' X ': 1} "    Print ' Func Local is:%s '% locals () x =1y =2Func ()' Both print {' __builtins__ ': <module ' __builtin__ ' (built-in), ' __file__ ': ' t.py ', ' Dog ': <class ' __main__. Dog ', ' __package__ ': None, ' func ': <function func at 0x7fc2985be7d0>, ' x ': 1, ' Y ': 2, ' __name__ ': ' __main__ ', ' _ _doc__ ': None} 'Print ' Local is:%s '% locals ()Print ' Globals is:%s '%globals () dog = Dog () dog.eat ()

Output

FuncLocalis: {' Wrap ': < function wrap  at 0x7febea277938, ' y ': 2, ' x ' : 1}Localis: {' __builtins__ ': <module' __builtin__ '(built-inch);' __file__ ':' t.py ',' Dog ': <class' __main__. Dog ',' __package__ ': None,' func ': < function func  at 0x7febea2777d0, ' x ': 1, ' Y ': 2, ' __name__ ': ' __main__ ', ' __doc__ ': None}Globalsis: {' __builtins__ ': <module' __builtin__ '(built-inch);' __file__ ':' t.py ',' Dog ': <class' __main__. Dog ',' __package__ ': None,' func ': < function func at  0x7febea2777d0, ' x ': 1, ' y ': 2, ' __name__ ': ' __main__ ', ' __doc__ ': None}{' h ': A,' K ': One,' self ': <__main__. Dog Object at 0x7febea271e10}

Python's Globals () and locals ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.