Re: [Python-Dev] PEP 448 review
On Mon, Mar 16, 2015 at 7:11 PM Neil Girdhar wrote: > Hi everyone, > > I was wondering what is left with the PEP 448 ( > http://bugs.python.org/issue2292) code review? Big thanks to Benjamin, > Ethan, and Serhiy for reviewing some (all?) of the code. What is the next > step of this process? > My suspicion is that if no one steps up between now and PyCon to do a complete code review of the final patch, we as a group will try to get it done at the PyCon sprints. I have made the issue a release blocker to help make sure it gets reviewed and doesn't accidentally get left behind. ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
[Python-Dev] Possible wrong behavior of the dict?
Hello! In order to explain, let define subclass of dict: class Pair: def __init__(self, key, val): self.key = key self.val = val class MyDict(dict): # def __init__(self, *args, **kwds): if len(args) > 1: raise TypeError('Expected at most 1 arguments, but got %d' % len(args)) for key, val in args[0]: self[key] = val for key, val in kwds.items(): self[key] = val def __getitem__(self, key): pair = dict.__getitem__(key) return pair.value def __setitem__(self, key, val): if key in self: pair = dict.__getitem__(key) pair.value = value else: pair = Pair(key, val) dict.__setitem__(self, key, pair) def values(self): for key in self: p = dict.__getitem__(self, key) yield p.value def items(self): for key, p in dict.__iter__(self): yield p.key, p.value The simple test give me strange result: >>> d = MyDict([('a', 1), ('b', 2), ('c', 3)]) >>> dict(d) {'a': <__main__.Pair at 0x104ca9e48>, 'b': <__main__.Pair at 0x104ca9e80>, 'c': <__main__.Pair at 0x104ca9eb8>} instead of {'a':1, 'b':2, 'c':3}. Is this right behavior of the dict? --- Zaur Shibzukhov ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Possible wrong behavior of the dict?
On Tue, Mar 17, 2015 at 3:05 PM Zaur Shibzukhov wrote: > Hello! > > In order to explain, let define subclass of dict: > > class Pair: > def __init__(self, key, val): > self.key = key > self.val = val > > class MyDict(dict): > # > def __init__(self, *args, **kwds): > if len(args) > 1: > raise TypeError('Expected at most 1 arguments, but got %d' % > len(args)) > > for key, val in args[0]: > self[key] = val > > for key, val in kwds.items(): > self[key] = val > > def __getitem__(self, key): > pair = dict.__getitem__(key) > return pair.value > > def __setitem__(self, key, val): > if key in self: > pair = dict.__getitem__(key) > pair.value = value > else: > pair = Pair(key, val) > dict.__setitem__(self, key, pair) > > def values(self): > for key in self: > p = dict.__getitem__(self, key) > yield p.value > > def items(self): > for key, p in dict.__iter__(self): > yield p.key, p.value > > > The simple test give me strange result: > > >>> d = MyDict([('a', 1), ('b', 2), ('c', 3)]) > >>> dict(d) > {'a': <__main__.Pair at 0x104ca9e48>, > 'b': <__main__.Pair at 0x104ca9e80>, > 'c': <__main__.Pair at 0x104ca9eb8>} > > instead of {'a':1, 'b':2, 'c':3}. > > > Is this right behavior of the dict? > 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. ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Possible wrong behavior of the dict?
Yes... But I expected that dict constructor will use `__getitem__` or `items` method of MyDict instance in order to retrieve items of the MyDict instance during construction of the dict instance... Instead it interpreted MyDict instance as the dict instance during construction of new dict.This exactly caused my confusion. --- *Zaur Shibzukhov* 2015-03-17 22:12 GMT+03:00 Brett Cannon : > > > On Tue, Mar 17, 2015 at 3:05 PM Zaur Shibzukhov wrote: > >> Hello! >> >> In order to explain, let define subclass of dict: >> >> class Pair: >> def __init__(self, key, val): >> self.key = key >> self.val = val >> >> class MyDict(dict): >> # >> def __init__(self, *args, **kwds): >> if len(args) > 1: >> raise TypeError('Expected at most 1 arguments, but got %d' % >> len(args)) >> >> for key, val in args[0]: >> self[key] = val >> >> for key, val in kwds.items(): >> self[key] = val >> >> def __getitem__(self, key): >> pair = dict.__getitem__(key) >> return pair.value >> >> def __setitem__(self, key, val): >> if key in self: >> pair = dict.__getitem__(key) >> pair.value = value >> else: >> pair = Pair(key, val) >> dict.__setitem__(self, key, pair) >> >> def values(self): >> for key in self: >> p = dict.__getitem__(self, key) >> yield p.value >> >> def items(self): >> for key, p in dict.__iter__(self): >> yield p.key, p.value >> >> >> The simple test give me strange result: >> >> >>> d = MyDict([('a', 1), ('b', 2), ('c', 3)]) >> >>> dict(d) >> {'a': <__main__.Pair at 0x104ca9e48>, >> 'b': <__main__.Pair at 0x104ca9e80>, >> 'c': <__main__.Pair at 0x104ca9eb8>} >> >> instead of {'a':1, 'b':2, 'c':3}. >> >> >> Is this right behavior of the dict? >> > > 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. > ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Possible wrong behavior of the dict?
On Tue, Mar 17, 2015 at 3:29 PM Zaur Shibzukhov wrote: > Yes... But I expected that dict constructor will use `__getitem__` or > `items` method of MyDict instance in order to retrieve items of the MyDict > instance during construction of the dict instance... Instead it interpreted > MyDict instance as the dict instance during construction of new dict.This > exactly caused my confusion. > It's because you subclassed dict. Copying is optimized to skip over using the methods you listed when the object is a dict and so we know the structure of the object at the C level. You can look at https://hg.python.org/cpython/file/22a0c925a7c2/Objects/dictobject.c#l1997 to see the actual code. -Brett > > --- > *Zaur Shibzukhov* > > > 2015-03-17 22:12 GMT+03:00 Brett Cannon : > >> >> >> On Tue, Mar 17, 2015 at 3:05 PM Zaur Shibzukhov wrote: >> >>> Hello! >>> >>> In order to explain, let define subclass of dict: >>> >>> class Pair: >>> def __init__(self, key, val): >>> self.key = key >>> self.val = val >>> >>> class MyDict(dict): >>> # >>> def __init__(self, *args, **kwds): >>> if len(args) > 1: >>> raise TypeError('Expected at most 1 arguments, but got %d' % >>> len(args)) >>> >>> for key, val in args[0]: >>> self[key] = val >>> >>> for key, val in kwds.items(): >>> self[key] = val >>> >>> def __getitem__(self, key): >>> pair = dict.__getitem__(key) >>> return pair.value >>> >>> def __setitem__(self, key, val): >>> if key in self: >>> pair = dict.__getitem__(key) >>> pair.value = value >>> else: >>> pair = Pair(key, val) >>> dict.__setitem__(self, key, pair) >>> >>> def values(self): >>> for key in self: >>> p = dict.__getitem__(self, key) >>> yield p.value >>> >>> def items(self): >>> for key, p in dict.__iter__(self): >>> yield p.key, p.value >>> >>> >>> The simple test give me strange result: >>> >>> >>> d = MyDict([('a', 1), ('b', 2), ('c', 3)]) >>> >>> dict(d) >>> {'a': <__main__.Pair at 0x104ca9e48>, >>> 'b': <__main__.Pair at 0x104ca9e80>, >>> 'c': <__main__.Pair at 0x104ca9eb8>} >>> >>> instead of {'a':1, 'b':2, 'c':3}. >>> >>> >>> Is this right behavior of the dict? >>> >> >> 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. >> > > ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Possible wrong behavior of the dict?
So in such cases it should not subclassed `dict`, but `collections.MutableMapping`, for example? --- *Zaur Shibzukhov* 2015-03-17 22:38 GMT+03:00 Brett Cannon : > > > On Tue, Mar 17, 2015 at 3:29 PM Zaur Shibzukhov wrote: > >> Yes... But I expected that dict constructor will use `__getitem__` or >> `items` method of MyDict instance in order to retrieve items of the MyDict >> instance during construction of the dict instance... Instead it interpreted >> MyDict instance as the dict instance during construction of new dict.This >> exactly caused my confusion. >> > > It's because you subclassed dict. Copying is optimized to skip over using > the methods you listed when the object is a dict and so we know the > structure of the object at the C level. You can look at > https://hg.python.org/cpython/file/22a0c925a7c2/Objects/dictobject.c#l1997 > to see the actual code. > > -Brett > > >> >> --- >> *Zaur Shibzukhov* >> >> >> 2015-03-17 22:12 GMT+03:00 Brett Cannon : >> >>> >>> >>> On Tue, Mar 17, 2015 at 3:05 PM Zaur Shibzukhov >>> wrote: >>> Hello! In order to explain, let define subclass of dict: class Pair: def __init__(self, key, val): self.key = key self.val = val class MyDict(dict): # def __init__(self, *args, **kwds): if len(args) > 1: raise TypeError('Expected at most 1 arguments, but got %d' % len(args)) for key, val in args[0]: self[key] = val for key, val in kwds.items(): self[key] = val def __getitem__(self, key): pair = dict.__getitem__(key) return pair.value def __setitem__(self, key, val): if key in self: pair = dict.__getitem__(key) pair.value = value else: pair = Pair(key, val) dict.__setitem__(self, key, pair) def values(self): for key in self: p = dict.__getitem__(self, key) yield p.value def items(self): for key, p in dict.__iter__(self): yield p.key, p.value The simple test give me strange result: >>> d = MyDict([('a', 1), ('b', 2), ('c', 3)]) >>> dict(d) {'a': <__main__.Pair at 0x104ca9e48>, 'b': <__main__.Pair at 0x104ca9e80>, 'c': <__main__.Pair at 0x104ca9eb8>} instead of {'a':1, 'b':2, 'c':3}. Is this right behavior of the dict? >>> >>> 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. >>> >> >> ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Possible wrong behavior of the dict?
On Tue, Mar 17, 2015 at 3:46 PM Zaur Shibzukhov wrote: > So in such cases it should not subclassed `dict`, but > `collections.MutableMapping`, for example? > Yes (see the comment at https://hg.python.org/cpython/file/22a0c925a7c2/Objects/dictobject.c#l2003 ). -Brett > > --- > *Zaur Shibzukhov* > > > 2015-03-17 22:38 GMT+03:00 Brett Cannon : > >> >> >> On Tue, Mar 17, 2015 at 3:29 PM Zaur Shibzukhov wrote: >> >>> Yes... But I expected that dict constructor will use `__getitem__` or >>> `items` method of MyDict instance in order to retrieve items of the MyDict >>> instance during construction of the dict instance... Instead it interpreted >>> MyDict instance as the dict instance during construction of new dict.This >>> exactly caused my confusion. >>> >> >> It's because you subclassed dict. Copying is optimized to skip over using >> the methods you listed when the object is a dict and so we know the >> structure of the object at the C level. You can look at >> https://hg.python.org/cpython/file/22a0c925a7c2/Objects/dictobject.c#l1997 >> to see the actual code. >> >> -Brett >> >> >>> >>> --- >>> *Zaur Shibzukhov* >>> >>> >>> 2015-03-17 22:12 GMT+03:00 Brett Cannon : >>> On Tue, Mar 17, 2015 at 3:05 PM Zaur Shibzukhov wrote: > Hello! > > In order to explain, let define subclass of dict: > > class Pair: > def __init__(self, key, val): > self.key = key > self.val = val > > class MyDict(dict): > # > def __init__(self, *args, **kwds): > if len(args) > 1: > raise TypeError('Expected at most 1 arguments, but got %d' > % len(args)) > > for key, val in args[0]: > self[key] = val > > for key, val in kwds.items(): > self[key] = val > > def __getitem__(self, key): > pair = dict.__getitem__(key) > return pair.value > > def __setitem__(self, key, val): > if key in self: > pair = dict.__getitem__(key) > pair.value = value > else: > pair = Pair(key, val) > dict.__setitem__(self, key, pair) > > def values(self): > for key in self: > p = dict.__getitem__(self, key) > yield p.value > > def items(self): > for key, p in dict.__iter__(self): > yield p.key, p.value > > > The simple test give me strange result: > > >>> d = MyDict([('a', 1), ('b', 2), ('c', 3)]) > >>> dict(d) > {'a': <__main__.Pair at 0x104ca9e48>, > 'b': <__main__.Pair at 0x104ca9e80>, > 'c': <__main__.Pair at 0x104ca9eb8>} > > instead of {'a':1, 'b':2, 'c':3}. > > > Is this right behavior of the dict? > 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. >>> >>> > ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Possible wrong behavior of the dict?
Thanks. --- *Zaur Shibzukhov* 2015-03-17 22:48 GMT+03:00 Brett Cannon : > > > On Tue, Mar 17, 2015 at 3:46 PM Zaur Shibzukhov wrote: > >> So in such cases it should not subclassed `dict`, but >> `collections.MutableMapping`, for example? >> > > Yes (see the comment at > https://hg.python.org/cpython/file/22a0c925a7c2/Objects/dictobject.c#l2003 > ). > > -Brett > > >> >> --- >> *Zaur Shibzukhov* >> >> >> 2015-03-17 22:38 GMT+03:00 Brett Cannon : >> >>> >>> >>> On Tue, Mar 17, 2015 at 3:29 PM Zaur Shibzukhov >>> wrote: >>> Yes... But I expected that dict constructor will use `__getitem__` or `items` method of MyDict instance in order to retrieve items of the MyDict instance during construction of the dict instance... Instead it interpreted MyDict instance as the dict instance during construction of new dict.This exactly caused my confusion. >>> >>> It's because you subclassed dict. Copying is optimized to skip over >>> using the methods you listed when the object is a dict and so we know the >>> structure of the object at the C level. You can look at >>> https://hg.python.org/cpython/file/22a0c925a7c2/Objects/dictobject.c#l1997 >>> to see the actual code. >>> >>> -Brett >>> >>> --- *Zaur Shibzukhov* 2015-03-17 22:12 GMT+03:00 Brett Cannon : > > > On Tue, Mar 17, 2015 at 3:05 PM Zaur Shibzukhov > wrote: > >> Hello! >> >> In order to explain, let define subclass of dict: >> >> class Pair: >> def __init__(self, key, val): >> self.key = key >> self.val = val >> >> class MyDict(dict): >> # >> def __init__(self, *args, **kwds): >> if len(args) > 1: >> raise TypeError('Expected at most 1 arguments, but got >> %d' % len(args)) >> >> for key, val in args[0]: >> self[key] = val >> >> for key, val in kwds.items(): >> self[key] = val >> >> def __getitem__(self, key): >> pair = dict.__getitem__(key) >> return pair.value >> >> def __setitem__(self, key, val): >> if key in self: >> pair = dict.__getitem__(key) >> pair.value = value >> else: >> pair = Pair(key, val) >> dict.__setitem__(self, key, pair) >> >> def values(self): >> for key in self: >> p = dict.__getitem__(self, key) >> yield p.value >> >> def items(self): >> for key, p in dict.__iter__(self): >> yield p.key, p.value >> >> >> The simple test give me strange result: >> >> >>> d = MyDict([('a', 1), ('b', 2), ('c', 3)]) >> >>> dict(d) >> {'a': <__main__.Pair at 0x104ca9e48>, >> 'b': <__main__.Pair at 0x104ca9e80>, >> 'c': <__main__.Pair at 0x104ca9eb8>} >> >> instead of {'a':1, 'b':2, 'c':3}. >> >> >> Is this right behavior of the dict? >> > > 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. > >> ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Possible wrong behavior of the dict?
* Zaur Shibzukhov [2015-03-17 22:29:07 +0300]: > Yes... But I expected that dict constructor will use `__getitem__` or > `items` method of MyDict instance in order to retrieve items of the MyDict > instance during construction of the dict instance... Instead it interpreted > MyDict instance as the dict instance during construction of new dict.This > exactly caused my confusion. Subclassing builtins is always a recipe for trouble, because the C implementation doesn't necessarily call your Python methods. You should probably use collections.UserDict or collections.abc.(Mutable)Mapping instead: https://docs.python.org/3/library/collections.html#collections.UserDict https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping Florian -- http://www.the-compiler.org | m...@the-compiler.org (Mail/XMPP) GPG: 916E B0C8 FD55 A072 | http://the-compiler.org/pubkey.asc I love long mails! | http://email.is-not-s.ms/ pgp57NLMvC4Lp.pgp Description: PGP signature ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] PEP 489: Redesigning extension module loading
On 16/03/2015 12:38, Petr Viktorin wrote: Hello, Can you use anything from the meta issue http://bugs.python.org/issue15787 for PEP 3121 and PEP 384 or will the work that you are doing render everything done previously redundant? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com