Re: [Tutor] subtyping builtin type

2014-01-02 Thread spir
On 01/02/2014 03:21 AM, Steven D'Aprano wrote: On Wed, Jan 01, 2014 at 02:49:17PM +0100, spir wrote: On 01/01/2014 01:26 AM, Steven D'Aprano wrote: On Tue, Dec 31, 2013 at 03:35:55PM +0100, spir wrote: [...] I take the opportunity to add a few features, but would do without Source altogether

Re: [Tutor] subtyping builtin type

2014-01-01 Thread Steven D'Aprano
On Wed, Jan 01, 2014 at 02:49:17PM +0100, spir wrote: > On 01/01/2014 01:26 AM, Steven D'Aprano wrote: > >On Tue, Dec 31, 2013 at 03:35:55PM +0100, spir wrote: [...] > I take the opportunity to add a few features, but would do > without Source altogether if it were not for 'i'. > The reason is: it

Re: [Tutor] subtyping builtin type

2014-01-01 Thread spir
On 01/01/2014 01:26 AM, Steven D'Aprano wrote: On Tue, Dec 31, 2013 at 03:35:55PM +0100, spir wrote: Hello, I don't remember exactly how to do that. As an example: class Source (str): __slots__ = ['i', 'n'] def __init__ (self, string): self.i = 0 # current m

Re: [Tutor] subtyping builtin type

2013-12-31 Thread eryksun
On Tue, Dec 31, 2013 at 7:26 PM, Steven D'Aprano wrote: > > from collections import namedtuple > > class Source(namedtuple("Source", "string i n")): > def __new__(cls, string, i=0, n=None): > if n is None: > n = len(string) > return super(Source, cls).__new__(cls, s

Re: [Tutor] subtyping builtin type

2013-12-31 Thread Steven D'Aprano
On Tue, Dec 31, 2013 at 03:35:55PM +0100, spir wrote: > Hello, > > I don't remember exactly how to do that. As an example: > > class Source (str): > __slots__ = ['i', 'n'] > def __init__ (self, string): > self.i = 0 # current matching index in source > sel

Re: [Tutor] subtyping builtin type

2013-12-31 Thread Mark Lawrence
On 31/12/2013 21:11, Mark Lawrence wrote: On 31/12/2013 17:53, eryksun wrote: Refer to the language reference: http://docs.python.org/3/reference/datamodel.html#notes-on-using-slots Not found on the windows CHM file. Looks like another bug report to keep our lazy, bone idle core developers

Re: [Tutor] subtyping builtin type

2013-12-31 Thread Mark Lawrence
On 31/12/2013 17:53, eryksun wrote: On Tue, Dec 31, 2013 at 11:21 AM, Mark Lawrence wrote: The glossary entry for __slots__ states "A declaration inside a class that saves memory by pre-declaring space for instance attributes and eliminating instance dictionaries. Though popular, the technique

Re: [Tutor] subtyping builtin type

2013-12-31 Thread spir
On 12/31/2013 06:53 PM, eryksun wrote: On Tue, Dec 31, 2013 at 11:21 AM, Mark Lawrence wrote: The glossary entry for __slots__ states "A declaration inside a class that saves memory by pre-declaring space for instance attributes and eliminating instance dictionaries. Though popular, the techniq

Re: [Tutor] subtyping builtin type

2013-12-31 Thread Zachary Ware
On Tue, Dec 31, 2013 at 11:53 AM, eryksun wrote: > Minor correction: > > It says str requires empty __slots__, but that's a bug in the docs. > It's referring to 2.x str. Else this thread wouldn't exist. In 3.x, > str is basically the unicode type from 2.x. Its __itemsize__ is 0 > because the char

Re: [Tutor] subtyping builtin type

2013-12-31 Thread eryksun
On Tue, Dec 31, 2013 at 11:21 AM, Mark Lawrence wrote: > The glossary entry for __slots__ states "A declaration inside a class that > saves memory by pre-declaring space for instance attributes and eliminating > instance dictionaries. Though popular, the technique is somewhat tricky to > get right

Re: [Tutor] subtyping builtin type

2013-12-31 Thread Zachary Ware
On Tue, Dec 31, 2013 at 10:21 AM, Mark Lawrence wrote: > The glossary entry for __slots__ states "A declaration inside a class that > saves memory by pre-declaring space for instance attributes and eliminating > instance dictionaries. Though popular, the technique is somewhat tricky to > get right

Re: [Tutor] subtyping builtin type

2013-12-31 Thread Mark Lawrence
On 31/12/2013 15:54, Zachary Ware wrote: On Tue, Dec 31, 2013 at 9:22 AM, spir wrote: Thank you, Oscar & Zachary. I guess thus the way it is done is correct (for my case), is it? Seems your last remark shows the source of my confusion: probably, in past times, I subtyped builtin types and overr

Re: [Tutor] subtyping builtin type

2013-12-31 Thread Zachary Ware
On Tue, Dec 31, 2013 at 9:22 AM, spir wrote: > Thank you, Oscar & Zachary. I guess thus the way it is done is correct (for > my case), is it? Seems your last remark shows the source of my confusion: > probably, in past times, I subtyped builtin types and overrided their > __new__, thus had to call

Re: [Tutor] subtyping builtin type

2013-12-31 Thread eryksun
On Tue, Dec 31, 2013 at 9:35 AM, spir wrote: > > I[n] particular, how does python know which param to take as > source string? (There could be other params to __init__.) You override __new__, and you might also have to override __init__, but not in this case. object.__init__ ignores the extra arg

Re: [Tutor] subtyping builtin type

2013-12-31 Thread spir
On 12/31/2013 04:03 PM, Zachary Ware wrote: On Tue, Dec 31, 2013 at 8:35 AM, spir wrote: Hello, I don't remember exactly how to do that. As an example: class Source (str): __slots__ = ['i', 'n'] def __init__ (self, string): self.i = 0 # current matching ind

Re: [Tutor] subtyping builtin type

2013-12-31 Thread Zachary Ware
On Tue, Dec 31, 2013 at 8:35 AM, spir wrote: > Hello, > > I don't remember exactly how to do that. As an example: > > class Source (str): > __slots__ = ['i', 'n'] > def __init__ (self, string): > self.i = 0 # current matching index in source > self.n = len(

Re: [Tutor] subtyping builtin type

2013-12-31 Thread Oscar Benjamin
On Dec 31, 2013 2:37 PM, "spir" wrote: > > Hello, > > I don't remember exactly how to do that. As an example: > > class Source (str): > __slots__ = ['i', 'n'] > def __init__ (self, string): > self.i = 0 # current matching index in source > self.n = len(stri

[Tutor] subtyping builtin type

2013-12-31 Thread spir
Hello, I don't remember exactly how to do that. As an example: class Source (str): __slots__ = ['i', 'n'] def __init__ (self, string): self.i = 0 # current matching index in source self.n = len(string)# number of ucodes (Unicode code points)