473,385 Members | 2,029 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,385 software developers and data experts.

__new__ woes with list

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 instantiated with a string, then run a method over it
to tokenize it in a meaningful way.

so how come this doesn't work??? if I do this:

a=TMP( 'some string' )

it does nothing more than list('some string') and seems to be ignoring
the custom __new__ method.

def convertDataToList( data ): return [1,2,3]
class TMP(list):
def __new__( cls, data ):
if isinstance(data, basestring):
new = convertDataToList( data )
return list.__new__( cls, new )

if isinstance(data, list):
return list.__new__( cls, data )
Nov 21 '08 #1
3 1370
macaronikazoo <ma***********@gmail.comwrites:
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 instantiated with a string, then run a method over it
to tokenize it in a meaningful way.

so how come this doesn't work??? if I do this:

a=TMP( 'some string' )

it does nothing more than list('some string') and seems to be ignoring
the custom __new__ method.

def convertDataToList( data ): return [1,2,3]
class TMP(list):
def __new__( cls, data ):
if isinstance(data, basestring):
new = convertDataToList( data )
return list.__new__( cls, new )

if isinstance(data, list):
return list.__new__( cls, data )
A list is mutable, its initialisation is done in __init__() not
__new__(). There was a recent post about this (in the last couple of
weeks).

--
Arnaud
Nov 21 '08 #2


ok thansk - i will search again. i did try searching but didn't find
anything relevant...
Nov 21 '08 #3
macaronikazoo <ma***********@gmail.comwrites:
ok thansk - i will search again. i did try searching but didn't find
anything relevant...
Here's a link to the thread on google groups:

http://groups.google.com/group/comp....ff4eabc6182858

Unfortunately two threads seem to be intertwined: scroll down to when
the thread is renamed "python bug when subclassing list?".

HTH

--
Arnaud
Nov 21 '08 #4

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

Similar topics

1
by: Michele Simionato | last post by:
Let me show first how does it work for tuples: >>> class MyTuple(tuple): .... def __new__(cls,strng): # implicit conversion string of ints => tuple .... return...
3
by: H Jansen | last post by:
I try to work out how to use __new__ and metaclass (or __metaclass__) mechanisms in order to change the base class at runtime. I haven't been successful so far, however, even after also reading...
5
by: could ildg | last post by:
As there is already __init__, why need a __new__? What can __new__ give us while __init__ can't? In what situations we should use __new__? And in what situations we must use __new__? Can __new__...
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...
1
by: Daniel Klein | last post by:
I've have a program that is using both of the methods below (in different classes of course) for initializing the class. The example below shows a class with the 2 methods with one commented out. ...
0
by: Clarence | last post by:
I'm having problems with JPype and am trying to change the way it creates Python classes as proxies for Java classes and interfaces. I'm trying to get around "inconsistent mro" problems, but in...
5
by: Sandra-24 | last post by:
Ok here's the problem, I'm modifying a 3rd party library (boto) to have more specific exceptions. I want to change S3ResponseError into about 30 more specific errors. Preferably I want to do this...
4
by: Steven D'Aprano | last post by:
When you call a new-style class, the __new__ method is called with the user-supplied arguments, followed by the __init__ method with the same arguments. I would like to modify the arguments...
0
by: Ethan Furman | last post by:
Greetings, List! I was browsing through the Decimal source today, and found this: # We're immutable, so use __new__ not __init__ def __new__. . . self = object.__new__(cls)
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.