mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
Rename "dictionary" (type and constructor) to "dict".
This commit is contained in:
parent
7ad2d1eb8e
commit
a427a2b8d0
10 changed files with 66 additions and 63 deletions
|
@ -175,7 +175,7 @@ def my_import(name):
|
||||||
\code{del \var{x}.\var{foobar}}.
|
\code{del \var{x}.\var{foobar}}.
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
\begin{funcdesc}{dictionary}{\optional{mapping-or-sequence}}
|
\begin{funcdesc}{dict}{\optional{mapping-or-sequence}}
|
||||||
Return a new dictionary initialized from the optional argument.
|
Return a new dictionary initialized from the optional argument.
|
||||||
If an argument is not specified, return a new empty dictionary.
|
If an argument is not specified, return a new empty dictionary.
|
||||||
If the argument is a mapping object, return a dictionary mapping the
|
If the argument is a mapping object, return a dictionary mapping the
|
||||||
|
@ -191,12 +191,12 @@ def my_import(name):
|
||||||
\code{\{1: 2, 2: 3\}}:
|
\code{\{1: 2, 2: 3\}}:
|
||||||
|
|
||||||
\begin{itemize}
|
\begin{itemize}
|
||||||
\item \code{dictionary(\{1: 2, 2: 3\})}
|
\item \code{dict(\{1: 2, 2: 3\})}
|
||||||
\item \code{dictionary(\{1: 2, 2: 3\}.items())}
|
\item \code{dict(\{1: 2, 2: 3\}.items())}
|
||||||
\item \code{dictionary(\{1: 2, 2: 3\}.iteritems())}
|
\item \code{dict(\{1: 2, 2: 3\}.iteritems())}
|
||||||
\item \code{dictionary(zip((1, 2), (2, 3)))}
|
\item \code{dict(zip((1, 2), (2, 3)))}
|
||||||
\item \code{dictionary([[2, 3], [1, 2]])}
|
\item \code{dict([[2, 3], [1, 2]])}
|
||||||
\item \code{dictionary([(i-1, i) for i in (2, 3)])}
|
\item \code{dict([(i-1, i) for i in (2, 3)])}
|
||||||
\end{itemize}
|
\end{itemize}
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
|
|
|
@ -48,7 +48,7 @@ class Repr:
|
||||||
s = s + self.repr1(x[i], level-1)
|
s = s + self.repr1(x[i], level-1)
|
||||||
if n > self.maxlist: s = s + ', ...'
|
if n > self.maxlist: s = s + ', ...'
|
||||||
return '[' + s + ']'
|
return '[' + s + ']'
|
||||||
def repr_dictionary(self, x, level):
|
def repr_dict(self, x, level):
|
||||||
n = len(x)
|
n = len(x)
|
||||||
if n == 0: return '{}'
|
if n == 0: return '{}'
|
||||||
if level <= 0: return '{...}'
|
if level <= 0: return '{...}'
|
||||||
|
|
|
@ -163,7 +163,7 @@ def dicts():
|
||||||
for i in d.__iter__(): l.append(i)
|
for i in d.__iter__(): l.append(i)
|
||||||
vereq(l, l1)
|
vereq(l, l1)
|
||||||
l = []
|
l = []
|
||||||
for i in dictionary.__iter__(d): l.append(i)
|
for i in dict.__iter__(d): l.append(i)
|
||||||
vereq(l, l1)
|
vereq(l, l1)
|
||||||
d = {1:2, 3:4}
|
d = {1:2, 3:4}
|
||||||
testunop(d, 2, "len(a)", "__len__")
|
testunop(d, 2, "len(a)", "__len__")
|
||||||
|
@ -173,20 +173,20 @@ def dicts():
|
||||||
|
|
||||||
def dict_constructor():
|
def dict_constructor():
|
||||||
if verbose:
|
if verbose:
|
||||||
print "Testing dictionary constructor ..."
|
print "Testing dict constructor ..."
|
||||||
d = dictionary()
|
d = dict()
|
||||||
vereq(d, {})
|
vereq(d, {})
|
||||||
d = dictionary({})
|
d = dict({})
|
||||||
vereq(d, {})
|
vereq(d, {})
|
||||||
d = dictionary(items={})
|
d = dict(items={})
|
||||||
vereq(d, {})
|
vereq(d, {})
|
||||||
d = dictionary({1: 2, 'a': 'b'})
|
d = dict({1: 2, 'a': 'b'})
|
||||||
vereq(d, {1: 2, 'a': 'b'})
|
vereq(d, {1: 2, 'a': 'b'})
|
||||||
vereq(d, dictionary(d.items()))
|
vereq(d, dict(d.items()))
|
||||||
vereq(d, dictionary(items=d.iteritems()))
|
vereq(d, dict(items=d.iteritems()))
|
||||||
for badarg in 0, 0L, 0j, "0", [0], (0,):
|
for badarg in 0, 0L, 0j, "0", [0], (0,):
|
||||||
try:
|
try:
|
||||||
dictionary(badarg)
|
dict(badarg)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
@ -196,37 +196,37 @@ def dict_constructor():
|
||||||
# one seemed better as a ValueError than a TypeError.
|
# one seemed better as a ValueError than a TypeError.
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
raise TestFailed("no TypeError from dictionary(%r)" % badarg)
|
raise TestFailed("no TypeError from dict(%r)" % badarg)
|
||||||
else:
|
else:
|
||||||
raise TestFailed("no TypeError from dictionary(%r)" % badarg)
|
raise TestFailed("no TypeError from dict(%r)" % badarg)
|
||||||
try:
|
try:
|
||||||
dictionary(senseless={})
|
dict(senseless={})
|
||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
raise TestFailed("no TypeError from dictionary(senseless={})")
|
raise TestFailed("no TypeError from dict(senseless={})")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dictionary({}, {})
|
dict({}, {})
|
||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
raise TestFailed("no TypeError from dictionary({}, {})")
|
raise TestFailed("no TypeError from dict({}, {})")
|
||||||
|
|
||||||
class Mapping:
|
class Mapping:
|
||||||
# Lacks a .keys() method; will be added later.
|
# Lacks a .keys() method; will be added later.
|
||||||
dict = {1:2, 3:4, 'a':1j}
|
dict = {1:2, 3:4, 'a':1j}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dictionary(Mapping())
|
dict(Mapping())
|
||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
raise TestFailed("no TypeError from dictionary(incomplete mapping)")
|
raise TestFailed("no TypeError from dict(incomplete mapping)")
|
||||||
|
|
||||||
Mapping.keys = lambda self: self.dict.keys()
|
Mapping.keys = lambda self: self.dict.keys()
|
||||||
Mapping.__getitem__ = lambda self, i: self.dict[i]
|
Mapping.__getitem__ = lambda self, i: self.dict[i]
|
||||||
d = dictionary(items=Mapping())
|
d = dict(items=Mapping())
|
||||||
vereq(d, Mapping.dict)
|
vereq(d, Mapping.dict)
|
||||||
|
|
||||||
# Init from sequence of iterable objects, each producing a 2-sequence.
|
# Init from sequence of iterable objects, each producing a 2-sequence.
|
||||||
|
@ -237,23 +237,23 @@ def dict_constructor():
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return iter([self.first, self.last])
|
return iter([self.first, self.last])
|
||||||
|
|
||||||
d = dictionary([AddressBookEntry('Tim', 'Warsaw'),
|
d = dict([AddressBookEntry('Tim', 'Warsaw'),
|
||||||
AddressBookEntry('Barry', 'Peters'),
|
AddressBookEntry('Barry', 'Peters'),
|
||||||
AddressBookEntry('Tim', 'Peters'),
|
AddressBookEntry('Tim', 'Peters'),
|
||||||
AddressBookEntry('Barry', 'Warsaw')])
|
AddressBookEntry('Barry', 'Warsaw')])
|
||||||
vereq(d, {'Barry': 'Warsaw', 'Tim': 'Peters'})
|
vereq(d, {'Barry': 'Warsaw', 'Tim': 'Peters'})
|
||||||
|
|
||||||
d = dictionary(zip(range(4), range(1, 5)))
|
d = dict(zip(range(4), range(1, 5)))
|
||||||
vereq(d, dictionary([(i, i+1) for i in range(4)]))
|
vereq(d, dict([(i, i+1) for i in range(4)]))
|
||||||
|
|
||||||
# Bad sequence lengths.
|
# Bad sequence lengths.
|
||||||
for bad in [('tooshort',)], [('too', 'long', 'by 1')]:
|
for bad in [('tooshort',)], [('too', 'long', 'by 1')]:
|
||||||
try:
|
try:
|
||||||
dictionary(bad)
|
dict(bad)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
raise TestFailed("no ValueError from dictionary(%r)" % bad)
|
raise TestFailed("no ValueError from dict(%r)" % bad)
|
||||||
|
|
||||||
def test_dir():
|
def test_dir():
|
||||||
if verbose:
|
if verbose:
|
||||||
|
@ -543,13 +543,13 @@ def spamdicts():
|
||||||
|
|
||||||
def pydicts():
|
def pydicts():
|
||||||
if verbose: print "Testing Python subclass of dict..."
|
if verbose: print "Testing Python subclass of dict..."
|
||||||
verify(issubclass(dictionary, dictionary))
|
verify(issubclass(dict, dict))
|
||||||
verify(isinstance({}, dictionary))
|
verify(isinstance({}, dict))
|
||||||
d = dictionary()
|
d = dict()
|
||||||
vereq(d, {})
|
vereq(d, {})
|
||||||
verify(d.__class__ is dictionary)
|
verify(d.__class__ is dict)
|
||||||
verify(isinstance(d, dictionary))
|
verify(isinstance(d, dict))
|
||||||
class C(dictionary):
|
class C(dict):
|
||||||
state = -1
|
state = -1
|
||||||
def __init__(self, *a, **kw):
|
def __init__(self, *a, **kw):
|
||||||
if a:
|
if a:
|
||||||
|
@ -561,12 +561,12 @@ def pydicts():
|
||||||
return self.get(key, 0)
|
return self.get(key, 0)
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
assert isinstance(key, type(0))
|
assert isinstance(key, type(0))
|
||||||
dictionary.__setitem__(self, key, value)
|
dict.__setitem__(self, key, value)
|
||||||
def setstate(self, state):
|
def setstate(self, state):
|
||||||
self.state = state
|
self.state = state
|
||||||
def getstate(self):
|
def getstate(self):
|
||||||
return self.state
|
return self.state
|
||||||
verify(issubclass(C, dictionary))
|
verify(issubclass(C, dict))
|
||||||
a1 = C(12)
|
a1 = C(12)
|
||||||
vereq(a1.state, 12)
|
vereq(a1.state, 12)
|
||||||
a2 = C(foo=1, bar=2)
|
a2 = C(foo=1, bar=2)
|
||||||
|
@ -801,7 +801,7 @@ def multi():
|
||||||
vereq(a.getstate(), 0)
|
vereq(a.getstate(), 0)
|
||||||
a.setstate(10)
|
a.setstate(10)
|
||||||
vereq(a.getstate(), 10)
|
vereq(a.getstate(), 10)
|
||||||
class D(dictionary, C):
|
class D(dict, C):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
type({}).__init__(self)
|
type({}).__init__(self)
|
||||||
C.__init__(self)
|
C.__init__(self)
|
||||||
|
@ -813,7 +813,7 @@ def multi():
|
||||||
vereq(d.getstate(), 0)
|
vereq(d.getstate(), 0)
|
||||||
d.setstate(10)
|
d.setstate(10)
|
||||||
vereq(d.getstate(), 10)
|
vereq(d.getstate(), 10)
|
||||||
vereq(D.__mro__, (D, dictionary, C, object))
|
vereq(D.__mro__, (D, dict, C, object))
|
||||||
|
|
||||||
# SF bug #442833
|
# SF bug #442833
|
||||||
class Node(object):
|
class Node(object):
|
||||||
|
@ -999,7 +999,7 @@ def errors():
|
||||||
if verbose: print "Testing errors..."
|
if verbose: print "Testing errors..."
|
||||||
|
|
||||||
try:
|
try:
|
||||||
class C(list, dictionary):
|
class C(list, dict):
|
||||||
pass
|
pass
|
||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
|
@ -1865,10 +1865,10 @@ def keywords():
|
||||||
vereq(unicode(string='abc', errors='strict'), u'abc')
|
vereq(unicode(string='abc', errors='strict'), u'abc')
|
||||||
vereq(tuple(sequence=range(3)), (0, 1, 2))
|
vereq(tuple(sequence=range(3)), (0, 1, 2))
|
||||||
vereq(list(sequence=(0, 1, 2)), range(3))
|
vereq(list(sequence=(0, 1, 2)), range(3))
|
||||||
vereq(dictionary(items={1: 2}), {1: 2})
|
vereq(dict(items={1: 2}), {1: 2})
|
||||||
|
|
||||||
for constructor in (int, float, long, complex, str, unicode,
|
for constructor in (int, float, long, complex, str, unicode,
|
||||||
tuple, list, dictionary, file):
|
tuple, list, dict, file):
|
||||||
try:
|
try:
|
||||||
constructor(bogus_keyword_arg=1)
|
constructor(bogus_keyword_arg=1)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
|
|
|
@ -11,21 +11,21 @@
|
||||||
from test_support import sortdict
|
from test_support import sortdict
|
||||||
import pprint
|
import pprint
|
||||||
|
|
||||||
class defaultdict(dictionary):
|
class defaultdict(dict):
|
||||||
def __init__(self, default=None):
|
def __init__(self, default=None):
|
||||||
dictionary.__init__(self)
|
dict.__init__(self)
|
||||||
self.default = default
|
self.default = default
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
try:
|
try:
|
||||||
return dictionary.__getitem__(self, key)
|
return dict.__getitem__(self, key)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return self.default
|
return self.default
|
||||||
|
|
||||||
def get(self, key, *args):
|
def get(self, key, *args):
|
||||||
if not args:
|
if not args:
|
||||||
args = (self.default,)
|
args = (self.default,)
|
||||||
return dictionary.get(self, key, *args)
|
return dict.get(self, key, *args)
|
||||||
|
|
||||||
def merge(self, other):
|
def merge(self, other):
|
||||||
for key in other:
|
for key in other:
|
||||||
|
@ -56,7 +56,7 @@ Here's the new type at work:
|
||||||
3.25
|
3.25
|
||||||
>>> print a[0] # a non-existant item
|
>>> print a[0] # a non-existant item
|
||||||
0.0
|
0.0
|
||||||
>>> a.merge({1:100, 2:200}) # use a dictionary method
|
>>> a.merge({1:100, 2:200}) # use a dict method
|
||||||
>>> print sortdict(a) # show the result
|
>>> print sortdict(a) # show the result
|
||||||
{1: 3.25, 2: 200}
|
{1: 3.25, 2: 200}
|
||||||
>>>
|
>>>
|
||||||
|
@ -111,23 +111,23 @@ just like classic classes:
|
||||||
>>>
|
>>>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class defaultdict2(dictionary):
|
class defaultdict2(dict):
|
||||||
__slots__ = ['default']
|
__slots__ = ['default']
|
||||||
|
|
||||||
def __init__(self, default=None):
|
def __init__(self, default=None):
|
||||||
dictionary.__init__(self)
|
dict.__init__(self)
|
||||||
self.default = default
|
self.default = default
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
try:
|
try:
|
||||||
return dictionary.__getitem__(self, key)
|
return dict.__getitem__(self, key)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return self.default
|
return self.default
|
||||||
|
|
||||||
def get(self, key, *args):
|
def get(self, key, *args):
|
||||||
if not args:
|
if not args:
|
||||||
args = (self.default,)
|
args = (self.default,)
|
||||||
return dictionary.get(self, key, *args)
|
return dict.get(self, key, *args)
|
||||||
|
|
||||||
def merge(self, other):
|
def merge(self, other):
|
||||||
for key in other:
|
for key in other:
|
||||||
|
@ -168,7 +168,7 @@ For instance of built-in types, x.__class__ is now the same as type(x):
|
||||||
<type 'list'>
|
<type 'list'>
|
||||||
>>> isinstance([], list)
|
>>> isinstance([], list)
|
||||||
1
|
1
|
||||||
>>> isinstance([], dictionary)
|
>>> isinstance([], dict)
|
||||||
0
|
0
|
||||||
>>> isinstance([], object)
|
>>> isinstance([], object)
|
||||||
1
|
1
|
||||||
|
|
|
@ -145,7 +145,7 @@ class ReprTests(unittest.TestCase):
|
||||||
def test_descriptors(self):
|
def test_descriptors(self):
|
||||||
eq = self.assertEquals
|
eq = self.assertEquals
|
||||||
# method descriptors
|
# method descriptors
|
||||||
eq(repr(dictionary.items), "<method 'items' of 'dictionary' objects>")
|
eq(repr(dict.items), "<method 'items' of 'dict' objects>")
|
||||||
# XXX member descriptors
|
# XXX member descriptors
|
||||||
# XXX attribute descriptors
|
# XXX attribute descriptors
|
||||||
# XXX slot descriptors
|
# XXX slot descriptors
|
||||||
|
|
|
@ -34,7 +34,7 @@ BufferType = type(buffer(''))
|
||||||
|
|
||||||
TupleType = tuple
|
TupleType = tuple
|
||||||
ListType = list
|
ListType = list
|
||||||
DictType = DictionaryType = dictionary
|
DictType = DictionaryType = dict
|
||||||
|
|
||||||
def _f(): pass
|
def _f(): pass
|
||||||
FunctionType = type(_f)
|
FunctionType = type(_f)
|
||||||
|
|
|
@ -4,6 +4,9 @@ XXX Planned XXX Release date: 14-Nov-2001
|
||||||
|
|
||||||
Type/class unification and new-style classes
|
Type/class unification and new-style classes
|
||||||
|
|
||||||
|
- The new builtin dictionary() constructor, and dictionary type, have
|
||||||
|
been renamed to dict. This reflects a decade of common usage.
|
||||||
|
|
||||||
- New-style classes can now have a __del__ method, which is called
|
- New-style classes can now have a __del__ method, which is called
|
||||||
when the instance is deleted (just like for classic classes).
|
when the instance is deleted (just like for classic classes).
|
||||||
|
|
||||||
|
|
|
@ -1784,7 +1784,7 @@ dict_init(PyObject *self, PyObject *args, PyObject *kwds)
|
||||||
static char *kwlist[] = {"items", 0};
|
static char *kwlist[] = {"items", 0};
|
||||||
int result = 0;
|
int result = 0;
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:dictionary",
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:dict",
|
||||||
kwlist, &arg))
|
kwlist, &arg))
|
||||||
result = -1;
|
result = -1;
|
||||||
|
|
||||||
|
@ -1804,10 +1804,10 @@ dict_iter(dictobject *dict)
|
||||||
}
|
}
|
||||||
|
|
||||||
static char dictionary_doc[] =
|
static char dictionary_doc[] =
|
||||||
"dictionary() -> new empty dictionary.\n"
|
"dict() -> new empty dictionary.\n"
|
||||||
"dictionary(mapping) -> new dict initialized from a mapping object's\n"
|
"dict(mapping) -> new dictionary initialized from a mapping object's\n"
|
||||||
" (key, value) pairs.\n"
|
" (key, value) pairs.\n"
|
||||||
"dictionary(seq) -> new dict initialized as if via:\n"
|
"dict(seq) -> new dictionary initialized as if via:\n"
|
||||||
" d = {}\n"
|
" d = {}\n"
|
||||||
" for k, v in seq:\n"
|
" for k, v in seq:\n"
|
||||||
" d[k] = v";
|
" d[k] = v";
|
||||||
|
@ -1815,7 +1815,7 @@ static char dictionary_doc[] =
|
||||||
PyTypeObject PyDict_Type = {
|
PyTypeObject PyDict_Type = {
|
||||||
PyObject_HEAD_INIT(&PyType_Type)
|
PyObject_HEAD_INIT(&PyType_Type)
|
||||||
0,
|
0,
|
||||||
"dictionary",
|
"dict",
|
||||||
sizeof(dictobject),
|
sizeof(dictobject),
|
||||||
0,
|
0,
|
||||||
(destructor)dict_dealloc, /* tp_dealloc */
|
(destructor)dict_dealloc, /* tp_dealloc */
|
||||||
|
|
|
@ -2484,7 +2484,7 @@ tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check that the use doesn't do something silly and unsafe like
|
/* Check that the use doesn't do something silly and unsafe like
|
||||||
object.__new__(dictionary). To do this, we check that the
|
object.__new__(dict). To do this, we check that the
|
||||||
most derived base that's not a heap type is this type. */
|
most derived base that's not a heap type is this type. */
|
||||||
staticbase = subtype;
|
staticbase = subtype;
|
||||||
while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
|
while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
|
||||||
|
|
|
@ -1848,7 +1848,7 @@ _PyBuiltin_Init(void)
|
||||||
#ifndef WITHOUT_COMPLEX
|
#ifndef WITHOUT_COMPLEX
|
||||||
SETBUILTIN("complex", &PyComplex_Type);
|
SETBUILTIN("complex", &PyComplex_Type);
|
||||||
#endif
|
#endif
|
||||||
SETBUILTIN("dictionary", &PyDict_Type);
|
SETBUILTIN("dict", &PyDict_Type);
|
||||||
SETBUILTIN("float", &PyFloat_Type);
|
SETBUILTIN("float", &PyFloat_Type);
|
||||||
SETBUILTIN("property", &PyProperty_Type);
|
SETBUILTIN("property", &PyProperty_Type);
|
||||||
SETBUILTIN("int", &PyInt_Type);
|
SETBUILTIN("int", &PyInt_Type);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue