Re: cause __init__ to return a different class?

2011-09-18 Thread Matthew Pounsett
On Sep 15, 1:54 am, Ryan Kelly r...@rfk.id.au wrote: To be friendlier to others reading your code, I would consider using a classmethod to create an alternative constructor: I finally got back to looking at this today. As it turns out, un- overriding __new__ in the child class is more

Re: cause __init__ to return a different class?

2011-09-15 Thread Steven D'Aprano
On Thu, 15 Sep 2011 03:20 pm Matthew Pounsett wrote: I'm wondering if there's a way in python to cause __init__ to return a class other than the one initially specified. My use case is that I'd like to have a superclass that's capable of generating an instance of a random subclass. You

Re: cause __init__ to return a different class?

2011-09-15 Thread Jonathan Hartley
Perhaps a more idiomatic way of achieving the same thing is to use a factory function, which returns instances of different classes: def PersonFactory(foo): if foo: return Person() else: return Child() Apologies if the code is messed up, I'm posting from Google

Re: cause __init__ to return a different class?

2011-09-15 Thread Matthew Pounsett
On Sep 15, 1:35 am, Chris Rebert c...@rebertia.com wrote: Override __new__() instead: http://docs.python.org/reference/datamodel.html#object.__new__ Aha.. thanks! The reference book I'm working from neglects to mention __new__, so I'd assumed __init__ was the constructor. It hadn't occurred

Re: cause __init__ to return a different class?

2011-09-15 Thread Matthew Pounsett
On Sep 15, 1:54 am, Ryan Kelly r...@rfk.id.au wrote: The above will do exactly what you want, but it's generally bad style unless you have a very specific use-case.  Is there a particular reason you need to magically return a subclass, rather than making this explicit in the code? To be

Re: cause __init__ to return a different class?

2011-09-15 Thread Miki Tebeka
I'd go for a factory function (http://en.wikipedia.org/wiki/Factory_method_pattern): def create(foo): return Child(foo) if foo else Parent() -- http://mail.python.org/mailman/listinfo/python-list

Re: cause __init__ to return a different class?

2011-09-15 Thread Arnaud Delobelle
On 15 September 2011 15:41, Matthew Pounsett matt.pouns...@gmail.com wrote: On Sep 15, 1:35 am, Chris Rebert c...@rebertia.com wrote: Override __new__() instead: http://docs.python.org/reference/datamodel.html#object.__new__ Aha.. thanks!  The reference book I'm working from neglects to

cause __init__ to return a different class?

2011-09-14 Thread Matthew Pounsett
I'm wondering if there's a way in python to cause __init__ to return a class other than the one initially specified. My use case is that I'd like to have a superclass that's capable of generating an instance of a random subclass. I've tried both returning the subclass (as I would when

Re: cause __init__ to return a different class?

2011-09-14 Thread Chris Rebert
On Wed, Sep 14, 2011 at 10:20 PM, Matthew Pounsett matt.pouns...@gmail.com wrote: I'm wondering if there's a way in python to cause __init__ to return a class other than the one initially specified.  My use case is that I'd like to have a superclass that's capable of generating an instance of

Re: cause __init__ to return a different class?

2011-09-14 Thread Ryan Kelly
On 15/09/11 15:35, Chris Rebert wrote: On Wed, Sep 14, 2011 at 10:20 PM, Matthew Pounsett matt.pouns...@gmail.com wrote: I'm wondering if there's a way in python to cause __init__ to return a class other than the one initially specified. My use case is that I'd like to have a superclass