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
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
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
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
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
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
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
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
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
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
10 matches
Mail list logo