472,982 Members | 2,116 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,982 software developers and data experts.

Re: python bug when subclassing list?

Hamish McKenzie wrote:
I want to write a Vector class and it makes the most sense to just
subclass list. I also want to be able to instantiate a vector using either:

Vector( 1, 2, 3 )
Vector( [1, 2, 3] )

so I have this:

class Vector(list):
def __new__( cls, *a ):
print a
return list.__new__(cls, a)
print 'broken'
return list.__new__(cls, list(a))

doing Vector( 1, 2, 3 ) on this class results in a TypeError – which
doesn’t seem to get caught by the try block (ie “broken” never gets
printed, and it never tries to

I can do pretty much the exact same code but inheriting from tuple
instead of list and it works fine.

is this a python bug? or am I doing something wrong?


I am not sure of the proper way to fix this issue, but the problem you
have is that lists do not have a __new__ method:

<type 'list'>

<built-in method __new__ of *type object* at 0x1E1D6A78>

<slot wrapper '__init__' of *'list' objects*>

Changing the __new__ to __init__ at least gets your code to run, but
then you have this:

--vector.Vector(1, 2, 3)
(1, 2, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "vector.py", line 15, in __init__
return list.__new__(cls, list(a))
TypeError: list.__new__(X): X is not a type object (Vector)

Good luck in your journey!
Nov 12 '08 #1
0 1035

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

Similar topics

by: Emile van Sebille | last post by:
QOTW: "If we get 2.3.3c1 out in early December, we could release 2.3.3 final before the end of the year, and start 2004 with a 100% bug-free codebase <wink>." -- Tim Peters "cjOr proWe vbCould...
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 241 open ( -6) / 2622 closed (+26) / 2863 total (+20) Bugs : 764 open ( +6) / 4453 closed (+38) / 5217 total (+44) RFE : 150 open...
by: Timothy Fitz | last post by:
While I agree that the Zen of Python is an amazingly concise list of truisms, I do not see any meaning in: Flat is better than nested. I strive for balance between flat and nested. Does anyone...
by: barnesc | last post by:
I added some recipes to the Python Cookbook: - listmixin Use ListMixin to create custom list classes from a small subset of list methods: ...
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=()=>{
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 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...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.