Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #105544
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Erik <python@lucidity.plus.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: |
| Date | Wed, 23 Mar 2016 11:56:27 +0000 |
| Lines | 64 |
| Message-ID | <mailman.51.1458734377.2244.python-list@python.org> (permalink) |
| References | <CAFWQgO=o-rPavakLZZBRxAthM5g7xE=Mn4hwcFPxTUvCEXn7SQ@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de RU5AzHSDUVSAU7MihCt24A5LWspgtn8olH6NhaEQYsfw== |
| Return-Path | <python@lucidity.plus.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'true,': 0.04; '"""': 0.05; 'false,': 0.07; 'behavior,': 0.09; 'python': 0.10; 'def': 0.13; "'a',": 0.16; 'dictionaries': 0.16; 'different?': 0.16; 'false:': 0.16; 'from:addr:python': 0.16; 'here).': 0.16; 'key):': 0.16; 'keys.': 0.16; 'magic': 0.16; 'nick': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reproduce': 0.16; 'true:': 0.16; '{true:': 0.16; 'wrote:': 0.16; 'to:2**1': 0.21; 'header:In-Reply- To:1': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'skip:" 20': 0.26; 'skip:_ 20': 0.26; 'skip:m 30': 0.27; 'define': 0.27; '(such': 0.27; 'skip:_ 10': 0.32; 'skip:. 10': 0.32; 'class': 0.33; 'wrap': 0.33; 'case,': 0.34; 'advice': 0.35; 'skip:( 30': 0.35; 'done': 0.35; 'false': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'there,': 0.37; 'desired': 0.37; 'missing': 0.37; 'things': 0.38; "didn't": 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'charset:windows-1252': 0.62; 'here:': 0.63; 'reverse': 0.66; 'start.': 0.84; 'subject::': 0.85 |
| X-CM-Score | 0.00 |
| X-CNFS-Analysis | v=2.1 cv=O6PEx0JW c=1 sm=1 tr=0 a=BtX40c5qGHjBwFgnk142hQ==:117 a=BtX40c5qGHjBwFgnk142hQ==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=N659UExz7-8A:10 a=uPZiAMpXAAAA:8 a=EQHHwDq7Z_JodcffC4oA:9 a=pILNOxqGKmIA:10 |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 |
| In-Reply-To | <CAFWQgO=o-rPavakLZZBRxAthM5g7xE=Mn4hwcFPxTUvCEXn7SQ@mail.gmail.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:105544 |
Show key headers only | View raw
Hi Nick,
On 23/03/16 03:17, Nick Eubank wrote:
> In other words:
>
> In[1]:
> d = {True: 'a', False: 'b'}
> d[0] = 'z'
> d[False]
>
> Out[1]:
> 'z'
[snip]
> Relatedly, if this is a desired behavior, any advice one how best to work
> with dictionaries when one wants "True" and 1 to be different?
See the accepted answer here:
http://stackoverflow.com/questions/3387691/python-how-to-perfectly-override-a-dict
If you use the class that the answer suggests and then define a subclass:
"""
class MyTransformedDict(TransformedDict):
__reverse_transform__ = {
"__True__": True,
"__False__": False,
}
def __keytransform__(self, key):
if key is True: return "__True__"
if key is False: return "__False__"
return key
def __iter__(self):
return (self.__reverse_transform__.get(k, k)
for k in super(MyTransformedDict, self).__iter__())
def __contains__(self, key):
return self.__keytransform__(key) in self.store
"""
Then as long as you wrap such dictionaries in a "MyTransformedDict()"
(choose a better name!), it will do what:
"""
d = MyTransformedDict({True: 'a', False: 'b'})
d[0] = 'z'
d[False]
"""
Note that in your case, I've added the reverse transformation from the
magic tokens so that iter(), .iterkeys() and .iteritems()) return the
original True and False keys.
I've also added __contains__, but that should be in the
"TransformedDict()" superclass (I didn't want to reproduce the whole of
that here).
There are things missing (such as a dict-like repr()) or not done
optimally there, but it might be a good start. This works on Python 2 and 3.
E.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Erik <python@lucidity.plus.com> - 2016-03-23 11:56 +0000
csiph-web