473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Overriding list.__new__

Let me show first how does it work for tuples:
class MyTuple(tuple): .... def __new__(cls,strng): # implicit conversion string of ints => tuple
.... return super(MyTuple,cls).__new__(cls,map(int,strng.split ())) MyTuple('1 2') (1, 2)

No wonder here, everything is fine. However, if I do the same for
lists I get the following:
class MyList(list): .... def __new__(cls,strng): #implicit conversion string of ints => tuple
.... return super(MyList,cls).__new__(cls,map(int,strng.split( ))) MyList('1 2') ['1', ' ', '2']

The same is true for
class MyList(list): .... def __new__(cls,strng):
.... return list.__new__(cls,map(int,strng.split())) MyList('1 2') ['1', ' ', '2']

therefore it is not a problem of super.
The 'map' expression does not seem to be executed or, if its executed,
it has no effect at all. If I replace 'map' with anything, still I have
the same result:
class MyList(list): .... def __new__(cls,strng):
.... return list.__new__(cls,map(int,[]) # !notice: empty list here! MyList('1 2') ['1', ' ', '2']

In other words I always get the result of
list('1 2')

['1', ' ', '2']

and it seems impossible to override list.__new__.

I am very puzzled about that; any suggestions?

Michele
Jul 18 '05 #1
1 2288

"Michele Simionato" <mi**@pitt.edu> wrote in message
news:22**************************@posting.google.c om...
Let me show first how does it work for tuples:
class MyTuple(tuple): ... def __new__(cls,strng): # implicit conversion string of ints => tuple ... return

super(MyTuple,cls).__new__(cls,map(int,strng.split ()))
MyTuple('1 2')

(1, 2)

No wonder here, everything is fine. However, if I do the same for
lists I get the following:


Values of immutable objects must be set when created, because they
cannot be changed thereafter. Mutable objects can be initialized in
the __init__() method. I suspect this is true of lists, so that
overriding __new__ for lists has no effect.

TJR
Jul 18 '05 #2

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

Similar topics

8
by: Simon Burton | last post by:
Python 2.2.2 (#2, Nov 24 2002, 11:41:06) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> class pair(tuple): .... def __init__(self,a,b): .... ...
3
by: Christoph Groth | last post by:
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQBAJmKufx2/njzvX5URAipnAKCUco5ZCl85xJ8YboHHJM3RBIqiGwCfX3/P...
10
by: Stuart McGraw | last post by:
I have a class A from a third party that I cannot change and is implemented in C. I derive my own class B from A and add a couple new methods and override a method. The problem is that A has a...
1
by: Adonis | last post by:
I am subclassing the dict object, and having it serialize itself to disk for later access, but I am having some trouble with accessing the self._filename attribute, when the object is first created...
5
by: Ken Schutte | last post by:
Hi, I'm been trying to create some custom classes derived from some of python's built-in types, like int and list, etc. I've run into some trouble, which I could explain with a couple simple...
6
by: c james | last post by:
Given a condition at the time a class is instantiated, I want to change how __call__ is used. From the example below, self.no is using self.yes but self.__call__ is not. Can someone please...
9
by: mrstevegross | last post by:
I ran into a weird behavior with lexical scope in Python. I'm hoping someone on this forum can explain it to me. Here's the situation: I have an Outer class. In the Outer class, I define a...
0
by: Ethan Furman | last post by:
Hamish McKenzie wrote: Greetings! 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: --list <type 'list'>
3
by: macaronikazoo | last post by:
i'm having a hell of a time getting this to work. basically I want to be able to instantiate an object using either a list, or a string, but the class inherits from list. if the class is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.