473,223 Members | 1,749 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,223 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 1365
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: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.