Re: [Python-Dev] subclassing builtin data structures

2015-02-14 Thread Steven D'Aprano
On Sat, Feb 14, 2015 at 01:26:36PM -0500, Alexander Belopolsky wrote: > On Sat, Feb 14, 2015 at 7:23 AM, Steven D'Aprano > wrote: > > > Why can't int, str, list, tuple etc. be more like datetime? > > > They are. In all these types, class methods call subclass constructors but > instance method

Re: [Python-Dev] subclassing builtin data structures

2015-02-14 Thread Alexander Belopolsky
On Sat, Feb 14, 2015 at 2:36 PM, Georg Brandl wrote: > > In the case of int, there is a good reason for this behavior - bool. In > python, > > we want True + True == 2. In numpy, where binary operations preserve > > subclasses, you have > > > import numpy > numpy.bool_(1) + numpy.bool

Re: [Python-Dev] subclassing builtin data structures

2015-02-14 Thread Neil Girdhar
Oops, I meant to call super if necessary: @classmethod def __make_me_cls__(cls, arg_cls, *args, **kwargs): if arg_cls is C: pass elif arg_cls is D: args, kwargs = modified_args_for_D(args, kwargs) elif arg_cls is E: args, kwargs =

Re: [Python-Dev] subclassing builtin data structures

2015-02-14 Thread Neil Girdhar
I think the __make_me__ pattern discussed earlier is still the most generic cooperative solution. Here it is with a classmethod version too: class C(D, E): def some_method(self): return __make_me__(self, C) def __make_me__(self, arg_cls, *args, **kwargs): if arg_cls is C:

Re: [Python-Dev] subclassing builtin data structures

2015-02-14 Thread Georg Brandl
On 02/14/2015 07:26 PM, Alexander Belopolsky wrote: > In the case of int, there is a good reason for this behavior - bool. In > python, > we want True + True == 2. In numpy, where binary operations preserve > subclasses, you have > import numpy numpy.bool_(1) + numpy.bool_(1) > True

Re: [Python-Dev] subclassing builtin data structures

2015-02-14 Thread Alexander Belopolsky
On Sat, Feb 14, 2015 at 7:23 AM, Steven D'Aprano wrote: > Why can't int, str, list, tuple etc. be more like datetime? They are. In all these types, class methods call subclass constructors but instance methods don't. >>> class Int(int): ... pass ... >>> Int.from_bytes(bytes([1,2,3]), 'big

Re: [Python-Dev] subclassing builtin data structures

2015-02-14 Thread Steve Dower
t; Sent: ‎2/‎14/‎2015 4:24 To: python-dev@python.org<mailto:python-dev@python.org> Subject: Re: [Python-Dev] subclassing builtin data structures On Fri, Feb 13, 2015 at 06:03:35PM -0500, Neil Girdhar wrote: > I personally don't think this is a big enough issue to warrant any changes, >

Re: [Python-Dev] subclassing builtin data structures

2015-02-14 Thread Steven D'Aprano
On Fri, Feb 13, 2015 at 06:03:35PM -0500, Neil Girdhar wrote: > I personally don't think this is a big enough issue to warrant any changes, > but I think Serhiy's solution would be the ideal best with one additional > parameter: the caller's type. Something like > > def __make_me__(self, cls, *ar

Re: [Python-Dev] subclassing builtin data structures

2015-02-14 Thread Nick Coghlan
On 14 Feb 2015 08:57, "Alexander Belopolsky" wrote: > > > On Fri, Feb 13, 2015 at 4:44 PM, Neil Girdhar wrote: >> >> Interesting: http://stackoverflow.com/questions/5490824/should-constructors-comply-with-the-liskov-substitution-principle > > > Let me humbly conjecture that the people who wrote t

Re: [Python-Dev] subclassing builtin data structures

2015-02-14 Thread Nick Coghlan
On 14 Feb 2015 07:39, "Isaac Schwabacher" wrote: > > On 15-02-13, Guido van Rossum wrote: > > Are you willing to wait 10 days for an answer? I'm out of round tuits for a while. > > IIUC, the argument is that the Liskov Substitution Principle is a statement about how objects of a subtype behave re

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Serhiy Storchaka
On 14.02.15 01:03, Neil Girdhar wrote: Now the derived class knows who is asking for a copy. In the case of defaultdict, for example, he can implement __make_me__ as follows: def __make_me__(self, cls, *args, **kwargs): if cls is dict: return default_dict(self.default_factory, *args, **kwa

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Serhiy Storchaka
On 14.02.15 03:12, Ethan Furman wrote: The third choice is to use different specially designed constructor. class A(int): --> class A(int): ... def __add__(self, other): ... return self.__make_me__(int(self) + int(other)) ... def __repr__(self): ... return 'A(%d)' % sel

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Isaac Schwabacher
Hmm... super *is* a problem, because if we super, we can end up asking superclasses that we then won't know how to mimic. So what we really want is to move the superclasses we can mimic to the front of the MRO. If none of those can indirectly mimic the base class, then we try the other classes i

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Isaac Schwabacher
On 15-02-13, Neil Girdhar wrote: > I personally don't think this is a big enough issue to warrant any > changes, but I think Serhiy's solution would be the ideal best with one > additional parameter: the caller's type. Something like > > def __make_me__(self, cls, *args, **kwargs) > > > and t

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Neil Girdhar
I think it works as Isaac explained if __make_me__ is an instance method that also accepts the calling class type. On Fri, Feb 13, 2015 at 8:12 PM, Ethan Furman wrote: > On 02/13/2015 02:31 PM, Serhiy Storchaka wrote: > > On 13.02.15 05:41, Ethan Furman wrote: > >> So there are basically two cho

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Ethan Furman
On 02/13/2015 02:31 PM, Serhiy Storchaka wrote: > On 13.02.15 05:41, Ethan Furman wrote: >> So there are basically two choices: >> >> 1) always use the type of the most-base class when creating new instances >> >> pros: >> - easy >> - speedy code >> - no possible tracebacks on

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Neil Girdhar
You're right. On Fri, Feb 13, 2015 at 7:55 PM, Isaac Schwabacher wrote: > On 15-02-13, Neil Girdhar wrote: > > Unlike a regular method, you would never need to call super since you > should know everyone that could be calling you. Typically, when you call > super, you have something like this:

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Isaac Schwabacher
On 15-02-13, Neil Girdhar wrote: > Unlike a regular method, you would never need to call super since you should > know everyone that could be calling you. Typically, when you call super, you > have something like this: > > A < B, C > > > B < D > > > so you end up with > > > mro: A, B, C,

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Greg Ewing
Isaac Schwabacher wrote: IIUC, the argument is that the Liskov Substitution Principle is a statement about how objects of a subtype behave relative to objects of a supertype, and it doesn't apply to constructors because they aren't behaviors of existing objects. Another way to say that is that

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Neil Girdhar
Unlike a regular method, you would never need to call super since you should know everyone that could be calling you. Typically, when you call super, you have something like this: A < B, C B < D so you end up with mro: A, B, C, D And then when A calls super and B calls super it gets C which i

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Neil Girdhar
I personally don't think this is a big enough issue to warrant any changes, but I think Serhiy's solution would be the ideal best with one additional parameter: the caller's type. Something like def __make_me__(self, cls, *args, **kwargs) and the idea is that any time you want to construct a typ

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Alexander Belopolsky
On Fri, Feb 13, 2015 at 4:44 PM, Neil Girdhar wrote: > Interesting: > http://stackoverflow.com/questions/5490824/should-constructors-comply-with-the-liskov-substitution-principle > Let me humbly conjecture that the people who wrote the top answers have background in less capable languages than P

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Serhiy Storchaka
On 13.02.15 05:41, Ethan Furman wrote: So there are basically two choices: 1) always use the type of the most-base class when creating new instances pros: - easy - speedy code - no possible tracebacks on new object instantiation cons: - a subclass that needs/wan

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Isaac Schwabacher
On 15-02-13, Neil Girdhar wrote: > Interesting:  > http://stackoverflow.com/questions/5490824/should-constructors-comply-with-the-liskov-substitution-principle You'd better believe I read that thread not 20 minutes ago. :) > On Fri, Feb 13, 2015 at 3:37 PM, Isaac Schwabacher > target="1">ischw

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Neil Girdhar
Interesting: http://stackoverflow.com/questions/5490824/should-constructors-comply-with-the-liskov-substitution-principle On Fri, Feb 13, 2015 at 3:37 PM, Isaac Schwabacher wrote: > On 15-02-13, Guido van Rossum wrote: > > Are you willing to wait 10 days for an answer? I'm out of round > tuits

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Isaac Schwabacher
On 15-02-13, Guido van Rossum wrote: > Are you willing to wait 10 days for an answer? I'm out of round tuits for > a while. IIUC, the argument is that the Liskov Substitution Principle is a statement about how objects of a subtype behave relative to objects of a supertype, and it doesn't apply

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Guido van Rossum
Are you willing to wait 10 days for an answer? I'm out of round tuits for a while. On Fri, Feb 13, 2015 at 10:22 AM, Alexander Belopolsky < alexander.belopol...@gmail.com> wrote: > > On Fri, Feb 13, 2015 at 1:19 PM, Alexander Belopolsky < > alexander.belopol...@gmail.com> wrote: > >> > >> FWIW yo

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Alexander Belopolsky
On Fri, Feb 13, 2015 at 1:19 PM, Alexander Belopolsky < alexander.belopol...@gmail.com> wrote: >> >> FWIW you're wrong when you claim that "a constructor is no different from any other method". Someone else should probably explain this (it's an old argument that's been thoroughly settled). > > > We

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Alexander Belopolsky
On Fri, Feb 13, 2015 at 1:11 PM, Guido van Rossum wrote: > > >> Note that the original pure python prototype of the datetime module had >> date.__add__ and friends call self.__class__(year, month, day). >> Unfortunately, it looks like the original sandbox did not survive the the >> hg conversion,

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Guido van Rossum
On Fri, Feb 13, 2015 at 10:02 AM, Alexander Belopolsky < alexander.belopol...@gmail.com> wrote: > > On Fri, Feb 13, 2015 at 12:35 PM, Guido van Rossum > wrote: > >> IIUC you're proposing that the base class should *try* to construct an >> instance of the subclass by calling the type with an argum

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Alexander Belopolsky
On Fri, Feb 13, 2015 at 12:35 PM, Guido van Rossum wrote: > IIUC you're proposing that the base class should *try* to construct an > instance of the subclass by calling the type with an argument, and fail if > it doesn't work. But that makes the whole thing brittle in the light of > changes to th

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Guido van Rossum
On Thu, Feb 12, 2015 at 8:58 PM, Ethan Furman wrote: > On 02/12/2015 08:01 PM, Guido van Rossum wrote: > > On Thu, Feb 12, 2015 at 7:41 PM, Ethan Furman wrote: > >> > >> 2) always use the type of self when creating new instances > >> > >>cons: > >> - if constructor signatures cha

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Ionel Cristian Mărieș
Can we at least make it use the constructor (if there's a custom one)? Seems like a reasonable compromise to me (let whoever implements a custom __new__ deal with argument variance). Eg, make it use a __new__ like this: >>> class FancyInt(int): ... def __new__(self, value): ... return

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Alexander Belopolsky
On Thu, Feb 12, 2015 at 11:01 PM, Guido van Rossum wrote: > >> 2) always use the type of self when creating new instances >> .. >>cons: >> - if constructor signatures change, must override all methods which >>create new objects >> > > Con for #2 is a showstopper. Forget about

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Jonas Wielicki
If I may humbly chime in this, with a hint... On 13.02.2015 05:01, Guido van Rossum wrote: > On Thu, Feb 12, 2015 at 7:41 PM, Ethan Furman wrote: >> [snip] >> 2) always use the type of self when creating new instances >> >>pros: >> - subclasses automatically maintain type >> - much

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Neil Girdhar
With Python's cooperative inheritance, I think you want to do everything through one constructor sending keyword arguments up the chain. The keyword arguments are popped off as needed. With this setup I don't think you need "overloaded constructors". Best, Neil On Fri, Feb 13, 2015 at 4:44 AM,

Re: [Python-Dev] subclassing builtin data structures

2015-02-13 Thread Jonas Wielicki
If I may humbly chime in this, with a hint... On 13.02.2015 05:01, Guido van Rossum wrote: > On Thu, Feb 12, 2015 at 7:41 PM, Ethan Furman wrote: >> [snip] >> 2) always use the type of self when creating new instances >> >>pros: >> - subclasses automatically maintain type >> - much

Re: [Python-Dev] subclassing builtin data structures

2015-02-12 Thread Ethan Furman
On 02/12/2015 08:01 PM, Guido van Rossum wrote: > On Thu, Feb 12, 2015 at 7:41 PM, Ethan Furman wrote: >> >> 2) always use the type of self when creating new instances >> >>cons: >> - if constructor signatures change, must override all methods which >>create new objec

Re: [Python-Dev] subclassing builtin data structures

2015-02-12 Thread Guido van Rossum
On Thu, Feb 12, 2015 at 7:41 PM, Ethan Furman wrote: > On 02/12/2015 06:39 PM, Alexander Belopolsky wrote: > > > In my view, a constructor is no different from any other method. If the > designers of the subclass decided to change the > > signature in an incompatible way, they should either over

Re: [Python-Dev] subclassing builtin data structures

2015-02-12 Thread Ethan Furman
On 02/12/2015 06:39 PM, Alexander Belopolsky wrote: > In my view, a constructor is no different from any other method. If the > designers of the subclass decided to change the > signature in an incompatible way, they should either override all methods > that create new objects or live with trac

Re: [Python-Dev] subclassing builtin data structures

2015-02-12 Thread Ethan Furman
On 02/12/2015 06:57 PM, Steven D'Aprano wrote: > On Thu, Feb 12, 2015 at 06:14:22PM -0800, Ethan Furman wrote: >> On 02/12/2015 05:46 PM, MRAB wrote: >>> On 2015-02-13 00:55, Guido van Rossum wrote: > Actually, the problem is that the base class (e.g. int) doesn't know how to construct a

Re: [Python-Dev] subclassing builtin data structures

2015-02-12 Thread Mark Roberts
> On Feb 12, 2015, at 18:40, Chris Angelico wrote: > > On Fri, Feb 13, 2015 at 12:46 PM, MRAB wrote: > class BaseInt: >> ... def __init__(self, value): >> ... self._value = value >> ... def __add__(self, other): >> ... return type(self)(self._value + other) > >> O

Re: [Python-Dev] subclassing builtin data structures

2015-02-12 Thread Steven D'Aprano
On Thu, Feb 12, 2015 at 06:14:22PM -0800, Ethan Furman wrote: > On 02/12/2015 05:46 PM, MRAB wrote: > > On 2015-02-13 00:55, Guido van Rossum wrote: > >> Actually, the problem is that the base class (e.g. int) doesn't know how > >> to construct an instance of the subclass -- there is no reason (in

Re: [Python-Dev] subclassing builtin data structures

2015-02-12 Thread Chris Angelico
On Fri, Feb 13, 2015 at 12:46 PM, MRAB wrote: class BaseInt: > ... def __init__(self, value): > ... self._value = value > ... def __add__(self, other): > ... return type(self)(self._value + other) On Fri, Feb 13, 2015 at 11:55 AM, Guido van Rossum wrote: > ... there

Re: [Python-Dev] subclassing builtin data structures

2015-02-12 Thread Alexander Belopolsky
On Thu, Feb 12, 2015 at 7:55 PM, Guido van Rossum wrote: > the problem is that the base class (e.g. int) doesn't know how to > construct an instance of the subclass -- there is no reason (in general) > why the signature of a subclass constructor should match the base class > constructor, and it o

Re: [Python-Dev] subclassing builtin data structures

2015-02-12 Thread Ethan Furman
On 02/12/2015 05:46 PM, MRAB wrote: > On 2015-02-13 00:55, Guido van Rossum wrote: >> On Thu, Feb 12, 2015 at 4:36 PM, Ethan Furman > > wrote: >> >> I suspect the last big hurdle to making built-in data structures >> nicely subclassable is the insistence of such t

Re: [Python-Dev] subclassing builtin data structures

2015-02-12 Thread MRAB
On 2015-02-13 00:55, Guido van Rossum wrote: On Thu, Feb 12, 2015 at 4:36 PM, Ethan Furman mailto:et...@stoneleaf.us>> wrote: I suspect the last big hurdle to making built-in data structures nicely subclassable is the insistence of such types to return new instances as the base class

Re: [Python-Dev] subclassing builtin data structures

2015-02-12 Thread Ethan Furman
On 02/12/2015 04:55 PM, Guido van Rossum wrote: > On Thu, Feb 12, 2015 at 4:36 PM, Ethan Furman > wrote: > > I suspect the last big hurdle to making built-in data structures nicely > subclassable is the insistence of such types to > return new instances as the

Re: [Python-Dev] subclassing builtin data structures

2015-02-12 Thread Guido van Rossum
On Thu, Feb 12, 2015 at 4:36 PM, Ethan Furman wrote: > I suspect the last big hurdle to making built-in data structures nicely > subclassable is the insistence of such types to > return new instances as the base class instead of the derived class. > > In case that wasn't clear ;) > > --> class My

[Python-Dev] subclassing builtin data structures

2015-02-12 Thread Ethan Furman
I suspect the last big hurdle to making built-in data structures nicely subclassable is the insistence of such types to return new instances as the base class instead of the derived class. In case that wasn't clear ;) --> class MyInt(int): ... def __repr__(self): ... return 'MyInt(%d)' % s