472,792 Members | 4,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,792 software developers and data experts.

Any gotchas in returning a subclass instance from __new__?

Hi All,

Is anything wrong with the following code?

class Superclass(object):
def __new__(cls):
# Questioning the statement below
return super(Superclass, cls).__new__(Subclass)
class Subclass(Superclass):
pass
if __name__ == '__main__':
instance = Superclass()
print instance

It works here, and even constructors for Subclass and Superclass are
invoked (in correct order), even though such behavior is not explicitly
stated here http://docs.python.org/ref/customization.html. So, am I
asking for trouble or is it /the/ way to go about transforming base
class into a factory?
Thank you,

Sergey.

Aug 1 '06 #1
1 4862
wrote:
Hi All,

Is anything wrong with the following code?

class Superclass(object):
def __new__(cls):
# Questioning the statement below
return super(Superclass, cls).__new__(Subclass)
class Subclass(Superclass):
pass
if __name__ == '__main__':
instance = Superclass()
print instance
The only problems I can think if is that if you create any other subclasses
of Superclass, or of Subclass for that matter then you'll never succeed in
creating any of them, and the constructor i.e. __new__ of the subclass
never gets called.

I've used this technique where the Superclass iterates through all of its
subclasses and uses the constructor parameters to choose the most
appropriate subclass to construct. Eventually though I changed it to use a
separate factory function: that way the __new__ methods get called in the
expected order.

So something like:

@classmethod
def fromMimetype(cls, m):
for subclass in Superclass._handlers():
if subclass.supportsMimetype(m):
return subclass(m)

@classmethod
def _handlers(cls):
for c in cls.__subclasses__():
for sub in c._handlers():
yield sub
yield c
It works here, and even constructors for Subclass and Superclass are
invoked (in correct order), even though such behavior is not explicitly
stated here http://docs.python.org/ref/customization.html. So, am I
asking for trouble or is it /the/ way to go about transforming base
class into a factory?
Thank you,
I think you meant the initialisers are invoked correctly (the constructor
is the __new__ method): this isn't suprising since the system simply calls
__init__ on the returned object so the correct thing just happens.

Aug 1 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Edward Diener | last post by:
Is there a way in Python to have the constructor of a class "return" another instance of the same class ? I am well aware of the fact that __init__ does not return anything, but I would love to do...
14
by: Arthur | last post by:
A bit inspired by the decorator discussions, I'm trying to tackle something I had been avoiding. Essentially I am trying to create a non-destructive tranformation of an instance of a class - is...
1
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using...
3
by: Larry Lard | last post by:
Hi, This seems to me like a problem that has a simple solution but I can't for the life of me work it out. Suppose I have a base class Employee Public Class Employee Public FullName As...
4
by: David Thielen | last post by:
Hi; I am writing a class that implements IDbConnection. The i/f defines a methof BeginTransaction() that returns an IDbTransaction. I want to define this as returning a DbTransaction (as...
11
by: David Thielen | last post by:
Hi; I am writing a class that implements IDbConnection. The i/f defines a method BeginTransaction() that returns an IDbTransaction. I want to define this as returning a DbTransaction (as...
11
by: Paulo da Silva | last post by:
I would like to implement something like this: class C1: def __init__(self,xxx): if ... : self.foo = foo self.bar = bar else: self=C1.load(xxx)
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.