Re: Subclassing array
That's great, thanks ! To put it short, when I create a Stimulus object, it first seek __new__() method. But if I don't define it, it looks for the one defined in Vector. This raises a problem because the parameters passed to Stimulus(params) aren't fitting with Vector parameters, raising an exception. That's why I have to use this *arg **kw syntax in order to allow my subclasses having any arguments without causing troubles. Am I right ? -- http://mail.python.org/mailman/listinfo/python-list
Re: Subclassing array
TG wrote: That's great, thanks ! To put it short, when I create a Stimulus object, it first seek __new__() method. But if I don't define it, it looks for the one defined in Vector. This raises a problem because the parameters passed to Stimulus(params) aren't fitting with Vector parameters, raising an exception. That's why I have to use this *arg **kw syntax in order to allow my subclasses having any arguments without causing troubles. Am I right ? To put it short : yes !-) -- bruno desthuilliers python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in '[EMAIL PROTECTED]'.split('@')]) -- http://mail.python.org/mailman/listinfo/python-list
Re: Subclassing array
TG [EMAIL PROTECTED] wrote: ... When I call Vector.__init__() in Stimulus, doesn't it also call __new__ ? I don't understand the detail of callings to __new__ and __init__ in python inheritance ... Calling a (new-style) class does __new__ first, THEN calls the class's __init__ on the resulting instance -- and the arguments you're passing when calling the class go to both __new__ and __init__. Alex -- http://mail.python.org/mailman/listinfo/python-list
Re: Subclassing array
Alex Martelli [EMAIL PROTECTED] wrote: TG [EMAIL PROTECTED] wrote: When I call Vector.__init__() in Stimulus, doesn't it also call __new__ ? I don't understand the detail of callings to __new__ and __init__ in python inheritance ... Calling a (new-style) class does __new__ first, THEN calls the class's __init__ on the resulting instance -- and the arguments you're passing when calling the class go to both __new__ and __init__. ... so you might want something like: class Vector(array): def __new__(cls,*args): return super(Vector,cls).__new__(cls,'f') -- \S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/ ___ | Frankly I have no feelings towards penguins one way or the other \X/ |-- Arthur C. Clarke her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump -- http://mail.python.org/mailman/listinfo/python-list
Re: Subclassing array
Sion Arrowsmith a écrit : Alex Martelli [EMAIL PROTECTED] wrote: TG [EMAIL PROTECTED] wrote: When I call Vector.__init__() in Stimulus, doesn't it also call __new__ ? I don't understand the detail of callings to __new__ and __init__ in python inheritance ... Calling a (new-style) class does __new__ first, THEN calls the class's __init__ on the resulting instance -- and the arguments you're passing when calling the class go to both __new__ and __init__. ... so you might want something like: class Vector(array): def __new__(cls,*args): return super(Vector,cls).__new__(cls,'f') And if you want to support named arguments: class Vector(array): def __new__(cls,*args, **kw): return super(Vector,cls).__new__(cls,'f') -- http://mail.python.org/mailman/listinfo/python-list