Re: [Tutor] use of __new__

2010-03-12 Thread spir
On Fri, 12 Mar 2010 22:56:37 +1100 Steven D'Aprano wrote: > You might be tempted to change the first reference to Unicode to cls as > well, but sadly that does not work. The reason is complicated, and to > be honest I don't remember it, but you will probably find it by > googling for "python s

Re: [Tutor] use of __new__

2010-03-12 Thread Steven D'Aprano
I've taken the liberty of replying back to the list rather than in private. Denis, if you mean to deliberately reply privately, please say so at the start of the email, otherwise I will assume it was an accident. On Fri, 12 Mar 2010 09:56:11 pm spir wrote: > Side-question: Why use super() whe

Re: [Tutor] use of __new__

2010-03-12 Thread spir
On Fri, 12 Mar 2010 12:27:02 +1100 Steven D'Aprano wrote: > On Fri, 12 Mar 2010 11:53:16 am Steven D'Aprano wrote: > > > I have tried to match the behaviour of the built-in unicode as close > > as I am able. See here: > > http://docs.python.org/library/functions.html#unicode > > And by doing so

Re: [Tutor] use of __new__

2010-03-11 Thread Steven D'Aprano
On Fri, 12 Mar 2010 06:03:35 am spir wrote: > Hello, > > I need a custom unicode subtype (with additional methods). [snip] Here's my second attempt, and a very simple test function that passes. Obviously you have to add your own additional methods :) class Unicode(unicode): """Unicode(strin

Re: [Tutor] use of __new__

2010-03-11 Thread Steven D'Aprano
On Fri, 12 Mar 2010 11:53:16 am Steven D'Aprano wrote: > I have tried to match the behaviour of the built-in unicode as close > as I am able. See here: > http://docs.python.org/library/functions.html#unicode And by doing so, I entirely forgot that you want to change the default encoding from 'as

Re: [Tutor] use of __new__

2010-03-11 Thread Steven D'Aprano
On Fri, 12 Mar 2010 11:26:19 am Alan Gauld wrote: > "spir" wrote > > > The issue is the object (self) is then a unicode one instead of my > > own type. > > I think you need to modify self in __new__ The method signature for __new__ is usually written as: def __new__(cls, args): because when

Re: [Tutor] use of __new__

2010-03-11 Thread Steven D'Aprano
On Fri, 12 Mar 2010 06:03:35 am spir wrote: > Hello, > > I need a custom unicode subtype (with additional methods). This will > not be directly used by the user, instead it is just for internal > purpose. I would like the type to be able to cope with either a byte > str or a unicode str as argument

Re: [Tutor] use of __new__

2010-03-11 Thread Alan Gauld
"spir" wrote The issue is the object (self) is then a unicode one instead of my own type. I think you need to modify self in __new__ class Unicode(unicode): Unicode.FORMAT = "utf8" def __new__(self, text, format=None): # text can be str or unicode format = Unicode.FOR

[Tutor] use of __new__

2010-03-11 Thread spir
Hello, I need a custom unicode subtype (with additional methods). This will not be directly used by the user, instead it is just for internal purpose. I would like the type to be able to cope with either a byte str or a unicode str as argument. In the first case, it needs to be first decoded. I

Re: [Tutor] use of __new__

2009-06-12 Thread Lie Ryan
spir wrote: > Le Fri, 12 Jun 2009 22:37:17 +1000, > Lie Ryan s'exprima ainsi: > > Right, thank you. I continued my trials and ended with seemingly working > code, close to yours. > > # case pattern is Klass: yield String instead > if isinstance(pattern,Klass): >

Re: [Tutor] use of __new__

2009-06-12 Thread Kent Johnson
On Fri, Jun 12, 2009 at 10:03 AM, spir wrote: > Don't you have a comment in the 'if' case, too? Namely that __init__ is not > invoked explicitely, while the docs clearly state: > > << f __new__() returns an instance of cls, then the new instance’s __init__() > method will be invoked like __init__

Re: [Tutor] use of __new__

2009-06-12 Thread spir
Le Fri, 12 Jun 2009 09:05:35 -0400, Kent Johnson s'exprima ainsi: > On Fri, Jun 12, 2009 at 8:37 AM, Lie Ryan wrote: > class Normal(object): > > ...     def __new__(cls, arg): > > ...         if arg: > > ...             return Special(arg) > > ...         else: > > ...             ret = supe

Re: [Tutor] use of __new__

2009-06-12 Thread Kent Johnson
On Fri, Jun 12, 2009 at 9:48 AM, spir wrote: > Right, thank you. I continued my trials and ended with seemingly working > code, close to yours. > >                # case pattern is Klass: yield String instead >                if isinstance(pattern,Klass): >                        self = String(pat

Re: [Tutor] use of __new__

2009-06-12 Thread spir
Le Fri, 12 Jun 2009 22:37:17 +1000, Lie Ryan s'exprima ainsi: > >>> class Normal(object): > ... def __new__(cls, arg): > ... if arg: > ... return Special(arg) > ... else: > ... ret = super(Normal, cls).__new__(cls) > ... ret.__init__(arg)

Re: [Tutor] use of __new__

2009-06-12 Thread spir
Le Fri, 12 Jun 2009 22:37:17 +1000, Lie Ryan s'exprima ainsi: > spir wrote: > > Hello, > > > > I have (again) some issue using __new__. > > What I need is basically to catch an object creation and yield an object > > of an alternate type, when a condition is met. > > > > Basically, the outline

Re: [Tutor] use of __new__

2009-06-12 Thread Kent Johnson
On Fri, Jun 12, 2009 at 8:37 AM, Lie Ryan wrote: class Normal(object): > ...     def __new__(cls, arg): > ...         if arg: > ...             return Special(arg) > ...         else: > ...             ret = super(Normal, cls).__new__(cls) > ...             ret.__init__(arg) > ...            

Re: [Tutor] use of __new__

2009-06-12 Thread Lie Ryan
spir wrote: > Hello, > > I have (again) some issue using __new__. > What I need is basically to catch an object creation and yield an object of > an alternate type, when a condition is met. > > Basically, the outline looks like: > > class Normal(object): > def __new__(cls, arg): > i

Re: [Tutor] use of __new__

2009-06-12 Thread A.T.Hofkamp
spir wrote: Hello, I have (again) some issue using __new__. What I need is basically to catch an object creation and yield an object of an alternate type, when a condition is met. Basically, the outline looks like: class Normal(object): def __new__(cls, arg): if cond(arg):

[Tutor] use of __new__

2009-06-12 Thread spir
Hello, I have (again) some issue using __new__. What I need is basically to catch an object creation and yield an object of an alternate type, when a condition is met. Basically, the outline looks like: class Normal(object): def __new__(cls, arg): if cond(arg): #