Re: Differences creating tuples and collections.namedtuples

2013-02-22 Thread Steven D'Aprano
On Tue, 19 Feb 2013 22:38:32 -0500, Terry Reedy wrote: > On 2/18/2013 7:18 PM, Steven D'Aprano wrote: >> Terry Reedy wrote: >> >>> On 2/18/2013 6:47 AM, John Reid wrote: >>> I was hoping namedtuples could be used as replacements for tuples in all instances. >>> >>> This is a mistake in

Re: Differences creating tuples and collections.namedtuples

2013-02-20 Thread Roy Smith
In article , Chris Angelico wrote: > On Wed, Feb 20, 2013 at 2:38 PM, Terry Reedy wrote: > > Liskov Substitution Principle (LSP): I met this over 15 years ago reading > > debates among OOP enthusiasts about whether Rectangle should be a subclass > > of Square or Square a subclass of Rectangle,

Re: Differences creating tuples and collections.namedtuples

2013-02-19 Thread Chris Angelico
On Wed, Feb 20, 2013 at 2:38 PM, Terry Reedy wrote: > Liskov Substitution Principle (LSP): I met this over 15 years ago reading > debates among OOP enthusiasts about whether Rectangle should be a subclass > of Square or Square a subclass of Rectangle, and similarly, whether Ostrich > can be a legi

Re: Differences creating tuples and collections.namedtuples

2013-02-19 Thread Terry Reedy
On 2/18/2013 7:18 PM, Steven D'Aprano wrote: Terry Reedy wrote: On 2/18/2013 6:47 AM, John Reid wrote: I was hoping namedtuples could be used as replacements for tuples in all instances. This is a mistake in the following two senses. First, tuple is a class with instances while namedtuple

Re: Differences creating tuples and collections.namedtuples

2013-02-19 Thread Steven D'Aprano
On Mon, 18 Feb 2013 23:48:46 -0800, raymond.hettinger wrote: [...] > If your starting point is an existing iterable such as s=['Guido', > 'BDFL', 1], you have a couple of choices: p=Person(*s) or > p=Person._make(s). The latter form was put it to help avoid unpacking > and repacking the argumen

Re: Differences creating tuples and collections.namedtuples

2013-02-19 Thread Steven D'Aprano
On Mon, 18 Feb 2013 23:48:46 -0800, raymond.hettinger wrote: [...] > If your starting point is an existing iterable such as s=['Guido', > 'BDFL', 1], you have a couple of choices: p=Person(*s) or > p=Person._make(s). The latter form was put it to help avoid unpacking > and repacking the argumen

Re: Differences creating tuples and collections.namedtuples

2013-02-19 Thread Steven D'Aprano
Pardon me for the double-post, if any, my news client appears to have eaten my first reply. On Mon, 18 Feb 2013 23:48:46 -0800, raymond.hettinger wrote: [...] > If your starting point is an existing iterable such as s=['Guido', > 'BDFL', 1], you have a couple of choices: p=Person(*s) or > p=Pe

Re: Differences creating tuples and collections.namedtuples

2013-02-19 Thread Steven D'Aprano
On Mon, 18 Feb 2013 23:48:46 -0800, raymond.hettinger wrote: [...] > If your starting point is an existing iterable such as s=['Guido', > 'BDFL', 1], you have a couple of choices: p=Person(*s) or > p=Person._make(s). The latter form was put it to help avoid unpacking > and repacking the argumen

Re: Differences creating tuples and collections.namedtuples

2013-02-19 Thread John Reid
On 19/02/13 01:47, alex23 wrote: > On Feb 18, 9:47 pm, John Reid wrote: >> See http://article.gmane.org/gmane.comp.python.ipython.user/10270 for more >> info. > > One quick workaround would be to use a tuple where required and then > coerce it back to Result when needed as such: > > def sleep

Re: Differences creating tuples and collections.namedtuples

2013-02-19 Thread John Reid
On 19/02/13 00:18, Steven D'Aprano wrote: > Terry Reedy wrote: > >> On 2/18/2013 6:47 AM, John Reid wrote: >> >>> I was hoping namedtuples could be used as replacements for tuples >> > in all instances. >> >> This is a mistake in the following two senses. First, tuple is a class >> with instan

Re: Differences creating tuples and collections.namedtuples

2013-02-19 Thread Gregory Ewing
Steven D'Aprano wrote: py> class MyDict(dict): ... @classmethod ... def fromkeys(cls, func): ... # Expects a callback function that gets called with no arguments ... # and returns two items, a list of keys and a default value. ... return super(MyDict, cls).fromkeys

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Gregory Ewing
Steven D'Aprano wrote: Terry Reedy wrote: In fact, one reason to subclass a class is to change the initialization api. That might be a reason that people give, but it's a bad reason from the perspective of interface contracts, duck-typing and the LSP. Only if you're going to pass the cla

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread raymond . hettinger
On Monday, February 18, 2013 6:09:16 AM UTC-8, John Reid wrote: > I'm aware how to construct the namedtuple and the tuple. My point was > that they use different syntaxes for the same operation and this seems > to break ipython. I was wondering if this is a necessary design feature > or perhaps jus

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Steven D'Aprano
Oscar Benjamin wrote: > On 19 February 2013 00:18, Steven D'Aprano > wrote: >> Terry Reedy wrote: >>> On 2/18/2013 6:47 AM, John Reid wrote: > [snip] Is this a problem with namedtuples, ipython or just a feature? >>> >>> With canSequence. If isinstance was available and the above were writte

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread alex23
On Feb 18, 9:47 pm, John Reid wrote: > See http://article.gmane.org/gmane.comp.python.ipython.user/10270 for more > info. One quick workaround would be to use a tuple where required and then coerce it back to Result when needed as such: def sleep(secs): import os, time, parallel_helper

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Oscar Benjamin
On 19 February 2013 00:18, Steven D'Aprano wrote: > Terry Reedy wrote: >> On 2/18/2013 6:47 AM, John Reid wrote: [snip] >>> Is this a problem with namedtuples, ipython or just a feature? >> >> With canSequence. If isinstance was available and the above were written >> before list and tuple could b

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Roy Smith
Terry Reedy wrote: > Initializaing a namedtuple from an iterable is unusual, and > hence gets the longer syntax. I I quick look through our codebase agrees with that. I found 27 namedtuple classes. 21 were initialized with MyTuple(x, y, z) syntax. Three used MyTuple(*data). Most interesti

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Steven D'Aprano
Terry Reedy wrote: > On 2/18/2013 6:47 AM, John Reid wrote: > >> I was hoping namedtuples could be used as replacements for tuples > > in all instances. > > This is a mistake in the following two senses. First, tuple is a class > with instances while namedtuple is a class factory that produces

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Terry Reedy
On 2/18/2013 6:47 AM, John Reid wrote: I was hoping namedtuples could be used as replacements for tuples > in all instances. This is a mistake in the following two senses. First, tuple is a class with instances while namedtuple is a class factory that produces classes. (One could think of n

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread John Reid
On 18/02/13 14:53, Oscar Benjamin wrote: > On 18 February 2013 14:23, John Reid wrote: > [snip] >> That said it would be nice to know the rationale for >> namedtuple.__new__ to have a different signature to tuple.__new__. I'm >> guessing namedtuple._make has a similar interface to tuple.__new__. D

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Oscar Benjamin
On 18 February 2013 14:23, John Reid wrote: [snip] > That said it would be nice to know the rationale for > namedtuple.__new__ to have a different signature to tuple.__new__. I'm > guessing namedtuple._make has a similar interface to tuple.__new__. Does > anyone know what the rationale was for thi

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread John Reid
On 18/02/13 14:15, Oscar Benjamin wrote: > On 18 February 2013 14:09, John Reid wrote: >> On 18/02/13 12:11, Dave Angel wrote: >>> On 02/18/2013 06:47 AM, John Reid wrote: Hi, I was hoping namedtuples could be used as replacements for tuples in all instances. There seem to be

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Oscar Benjamin
On 18 February 2013 13:51, John Reid wrote: > On 18/02/13 12:03, Oscar Benjamin wrote: >> On 18 February 2013 11:47, John Reid wrote: >>> Hi, >>> >>> I was hoping namedtuples could be used as replacements for tuples in all >>> instances. >> namedtuples are not really intended to serves as tuples

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread John Reid
On 18/02/13 14:12, Oscar Benjamin wrote: > On 18 February 2013 13:51, John Reid wrote: >> On 18/02/13 12:03, Oscar Benjamin wrote: >>> On 18 February 2013 11:47, John Reid wrote: Hi, I was hoping namedtuples could be used as replacements for tuples in all instances. >>> name

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Oscar Benjamin
On 18 February 2013 14:09, John Reid wrote: > On 18/02/13 12:11, Dave Angel wrote: >> On 02/18/2013 06:47 AM, John Reid wrote: >>> Hi, >>> >>> I was hoping namedtuples could be used as replacements for tuples in >>> all instances. There seem to be some differences between how tuples >>> and namedt

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread John Reid
On 18/02/13 12:11, Dave Angel wrote: > On 02/18/2013 06:47 AM, John Reid wrote: >> Hi, >> >> I was hoping namedtuples could be used as replacements for tuples in >> all instances. There seem to be some differences between how tuples >> and namedtuples are created. For example with a tuple I can do:

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread John Reid
On 18/02/13 12:03, Oscar Benjamin wrote: > On 18 February 2013 11:47, John Reid wrote: >> Hi, >> >> I was hoping namedtuples could be used as replacements for tuples in all >> instances. > namedtuples are not really intended to serves as tuples anywhere. They > are intended to provide lightweight

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread John Reid
On 18/02/13 12:05, Oscar Benjamin wrote: > On 18 February 2013 12:03, Oscar Benjamin wrote: >> On 18 February 2013 11:47, John Reid wrote: >>> I'm seeing this problem because of the following code in IPython: >>> >>> def canSequence(obj): >>> if isinstance(obj, (list, tuple)): >>> t

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Dave Angel
On 02/18/2013 06:47 AM, John Reid wrote: Hi, I was hoping namedtuples could be used as replacements for tuples in all instances. There seem to be some differences between how tuples and namedtuples are created. For example with a tuple I can do: a=tuple([1,2,3]) with namedtuples I get a Type

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Oscar Benjamin
On 18 February 2013 12:03, Oscar Benjamin wrote: > On 18 February 2013 11:47, John Reid wrote: >> I'm seeing this problem because of the following code in IPython: >> >> def canSequence(obj): >> if isinstance(obj, (list, tuple)): >> t = type(obj) >> return t([can(i) for i in o

Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Oscar Benjamin
On 18 February 2013 11:47, John Reid wrote: > Hi, > > I was hoping namedtuples could be used as replacements for tuples in all > instances. namedtuples are not really intended to serves as tuples anywhere. They are intended to provide lightweight, immutable, hashable objects with *named* (rather

Differences creating tuples and collections.namedtuples

2013-02-18 Thread John Reid
Hi, I was hoping namedtuples could be used as replacements for tuples in all instances. There seem to be some differences between how tuples and namedtuples are created. For example with a tuple I can do: a=tuple([1,2,3]) with namedtuples I get a TypeError: from collections import namedtuple