The function vars()
returns a dictionary containing the current
local variables. With a module as argument, it returns that module's
global variables. The old function dir(x)
returns
vars(x).keys()
.
The function round(x)
returns a floating point number rounded
to the nearest integer (but still expressed as a floating point
number). E.g. round(3.4) == 3.0
and round(3.5) == 4.0
.
With a second argument it rounds to the specified number of digits,
e.g. round(math.pi, 4) == 3.1416
or even
round(123.4, -2) == 100.0
.
The function hash(x)
returns a hash value for an object.
All object types acceptable as dictionary keys have a hash value (and
it is this hash value that the dictionary implementation uses).
The function id(x)
return a unique identifier for an object.
For two objects x and y, id(x) == id(y)
if and only if
x is y
. (In fact the object's address is used.)
The function hasattr(x, name)
returns whether an object has an
attribute with the given name (a string value). The function
getattr(x, name)
returns the object's attribute with the given
name. The function setattr(x, name, value)
assigns a value to
an object's attribute with the given name. These three functions are
useful if the attribute names are not known beforehand. Note that
getattr(x, 'foo')
is equivalent to x.foo
, and
setattr(x, 'foo', y)
is equivalent to x.foo = y
. By
definition, hasattr(x, name)
returns true if and only if
getattr(x, name)
returns without raising an exception.