Python built-in functions (66) -- vars, python built-in 66 vars
English document:
-
vars
([
Object])
-
Return
__dict__
Attribute for a module, class, instance, or any other object with
__dict__
Attribute.
-
Objects such as modules and instances have an updateable
__dict__
Attribute; however, other objects may have write restrictions on their
__dict__
Attributes (for example, classes use a dictproxy to prevent direct dictionary updates ).
-
Without an argument,
vars()
Acts like
locals()
. Note, the locals dictionary is only useful for reads since updates to the locals dictionary are ignored.
Description
1. When a function does not receive parameters, the function returns local variables in the current scope in the same way as the locals function.
# The same as the locals function without parameters >>> v1 = vars () >>> l1 = locals () >>>> v1 {'_ name __': '_ main _', '_ builtins _': <module 'builtins '(built-in)>, 'v1 ':{...}, 'l1 ':{...}, '_ spec _': None, '_ doc _': None, '_ package _': None, '_ loader __': <class '_ frozen_importlib.BuiltinImporter' >}>> l1 {'_ name _': '_ main _', '_ builtins __': <module 'builtins '(built-in)>, 'v1 ':{...}, 'l1 ':{...}, '_ spec _': None, '_ doc _': None, '_ package _': None, '_ loader __': <class '_ frozen_importlib.BuiltinImporter'>}
2. When a function receives a parameter, the parameter can be a module, class, or class instance, or an object that defines the _ dict _ attribute.
# Apply to the module >>> import time >>> vars (time) {'gmtime': <built-in function gmtime>, 'tzname ': ('ö~~ú± ± × ¼ ± ¼ ä', 'ö~~ú~äáá ± '), 'timezone':-28800, 'struct _ time': <class 'time. struct_time '>, 'ctime': <built-in function ctime>, 'perf _ counter': <built-in function perf_counter>, 'mktime ': <built-in function mktime>, 'localtime': <built-in function localtime>, 'time': <built-in function time>, '_ package __': '', 'altzone':-32400, 'clock ': <built-in function clock>, 'strptime': <built-in function strptime>, 'monotonic ': <built-in function monotonic>, '_ loader _': <class '_ frozen_importlib.BuiltinImporter'>, 'Get _ clock_info ': <built-in function get_clock_info>, 'Sleep ': <built-in function sleep>, 'process _ time': <built-in function process_time>,' _ name _ ': 'time ', '_ STRUCT_TM_ITEMS': 9, '_ spec _': ModuleSpec (name = 'time', loader = <class '_ frozen_importlib.BuiltinImporter'>, origin = 'Built-in'), '_ doc _': 'This module provides varous functions to manipulate time values. \ n \ nThere are two standard representations of time. one is the number \ nof seconds since the Epoch, in UTC (a.k. a. GMT ). it may be an integer \ nor a floating point number (to represent fractions of seconds ). \ nThe Epoch is system-defined; on Unix, it is generally January 1st, 1970. \ nThe actual value can be retrieved by calling gmtime (0 ). \ n \ nThe other representation is a tuple of 9 integers giving local time. \ nThe tuple items are: \ n year (including century, e.g. 1998) \ n month (1-12) \ n day (1-31) \ n hours (0-23) \ n minutes (0-59) \ n seconds (0-59) \ n weekday (0-6, Monday is 0) \ n Julian day (day in the year, 1-366) \ n DST (Daylight Savings Time) flag (-1, 0 or 1) \ nIf the DST flag is 0, the time is given in the regular time zone; \ nif it is 1, the time is given in the DST time zone; \ nif it is-1, mktime () shoshould guess based on the date and time. \ n \ nVariables: \ n \ ntimezone -- difference in seconds between UTC and local standard time \ naltzone -- difference in seconds between UTC and local DST time \ ndaylight -- whether local time shocould reflect DST \ ntzname -- tuple (standard time zone name, DST time zone name) \ n \ nFunctions: \ n \ ntime () -- return current time in seconds since the Epoch as a float \ nclock () -- return CPU time since process start as a float \ nsleep () -- delay for a number of seconds given as a float \ ngmtime () -- convert seconds since Epoch to UTC tuple \ nlocaltime () -- convert seconds since Epoch to local time tuple \ nasctime () -- convert time tuple to string \ nctime () -- convert time in seconds to string \ nmktime () -- convert local time tuple to seconds since Epoch \ nstrftime () -- convert time tuple to string according to format specification \ nstrptime () -- parse string to time tuple according to format specification \ ntzset () -- change the local timezone ', 'strftime': <built-in function strftime>, 'asctime ': <built-in function asctime>, 'daylight ': 0} # act on class >>> vars (slice) mappingproxy ({' _ ne __': <slot wrapper '_ ne _' of 'slice 'objects>, '_ getattribute __': <slot wrapper '_ getattribute _' of 'slice 'objects>, '_ reduce __': <method '_ reduce _' of 'slice 'objects>, 'start': <member 'start' of 'slice 'objects>, 'indices ': <method 'indices' of 'slice 'objects>, '_ ge _': <slot wrapper '_ ge _' of 'slice 'objects>, 'stop': <member 'stop' of 'slice 'objects>, '_ eq __': <slot wrapper '_ eq _' of 'slice 'objects>, 'step': <member 'step' of 'slice 'objects>, '_ hash _': None, '_ doc _': 'slice (stop) \ nslice (start, stop [, step]) \ n \ nCreate a slice object. this is used for extended slicing (e.g. a []). ',' _ repr _ ': <slot wrapper' _ repr _ 'of 'slice 'objects>,' _ le __': <slot wrapper '_ le _' of 'slice 'objects>, '_ gt __': <slot wrapper '_ gt _' of 'slice 'objects>, '_ new __': <built-in method _ new _ of type object at 0x6A91B420>, '_ lt __': <slot wrapper '_ lt _' of 'slice 'objects >}) # applies to class instances >>> class A (object): pass >>>. _ dict __{}>>> vars (a) {}>>. name = 'Kim '>. _ dict __{ 'name': 'Kim '} >>> vars (a) {'name': 'Kim '}