Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #2802
| From | Raymond Hettinger <python@rcn.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Why is __root checked for in OrderedDict? |
| Date | 2011-04-07 12:30 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <2b653902-d1c3-4c0c-a788-65dce110e86b@f30g2000yqa.googlegroups.com> (permalink) |
| References | <8dd32ba4-11c5-4326-8e1a-535decc7cfe4@glegroupsg2000goo.googlegroups.com> |
On Apr 7, 4:13 am, andrew cooke <and...@acooke.org> wrote:
> If you look at the code inhttp://hg.python.org/cpython/file/6adbf5f3dafb/Lib/collections/__init...the attribute __root is checked for, and only created if missing. Why?
>
> I ask because, from what I understand, the __init__ method will only be called when the object is first being created, so __root will always be missing.
First of all, three cheers for reading the source code!
A user can call __init__() even after the OrderedDict instance has
already been created. If so, the __root attribute will already exist
and the whole operation becomes equivalent to an update().
You can see the same behavior with regular dictionaries:
>>> d = dict(a=1, b=2)
>>> d.__init__(b=4, c=5)
>>> d
{'a': 1, 'c': 5, 'b': 4}
Raymond
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Why is __root checked for in OrderedDict? andrew cooke <andrew@acooke.org> - 2011-04-07 04:13 -0700 Re: Why is __root checked for in OrderedDict? Raymond Hettinger <python@rcn.com> - 2011-04-07 12:30 -0700
csiph-web