A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-dev/attachments/20150317/0f90b3ce/attachment.html below:

<div dir="ltr"><br><br><div class="gmail_quote">On Tue, Mar 17, 2015 at 3:05 PM Zaur Shibzukhov <<a href="mailto:szport@gmail.com">szport@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello!<br><br>In order to explain, let define subclass of dict:<br><br>class Pair:<br>  Â  def __init__(self, key, val):<br>  Â  Â  Â  self.key = key<br>  Â  Â  Â  self.val = val<br><br>class MyDict(dict):<br>  Â  #<br>  Â  def __init__(self, *args, **kwds):<br>  Â  Â  Â  if len(args) > 1:<br>  Â  Â  Â  Â  Â  raise TypeError('Expected at most 1 arguments, but got %d' % len(args))<br><br>  Â  Â  Â  for key, val in args[0]:<br>  Â  Â  Â  Â  Â  self[key] = val<br>  Â  Â  Â  Â  Â <br>  Â  Â  Â  for key, val in kwds.items():<br>  Â  Â  Â  Â  Â  self[key] = val<br><br>  Â  def __getitem__(self, key):<br>  Â  Â  Â  pair = dict.__getitem__(key)<br>  Â  Â  Â  return pair.value<br><br>  Â  def __setitem__(self, key, val):<br>  Â  Â  Â  if key in self:<br>  Â  Â  Â  Â  Â  pair = dict.__getitem__(key)<br>  Â  Â  Â  Â  Â  pair.value = value<br>  Â  Â  Â  else:<br>  Â  Â  Â  Â  Â  pair = Pair(key, val)<br>  Â  Â  Â  Â  Â  dict.__setitem__(self, key, pair)<br><br>  Â  def values(self):<br>  Â  Â  Â  for key in self:<br>  Â  Â  Â  Â  Â  p = dict.__getitem__(self, key)<br>  Â  Â  Â  Â  Â  yield p.value<br><br>  Â  def items(self):<br>  Â  Â  Â  for key, p in dict.__iter__(self):<br>  Â  Â  Â  Â  Â  yield p.key, p.value<br><br><br>The simple test give me strange result:<br><br>>>> d = MyDict([('a', 1), ('b', 2), ('c', 3)])<br>>>> dict(d)<br>{'a': <__main__.Pair at 0x104ca9e48>,<br> 'b': <__main__.Pair at 0x104ca9e80>,<br> 'c': <__main__.Pair at 0x104ca9eb8>}<br><br>instead of {'a':1, 'b':2, 'c':3}.<br><br><br>Is this right behavior of the dict?<br></div></blockquote><div><br></div><div>Yes because in your __setitem__ call you are storing the value as the Pair. So when dict prints its repr it prints the key and value, and in this case the value is a Pair.</div></div></div>

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4