class MyList(list):

    def __new__(cls, *p, **k):
        if not '_the_instance' in cls.__dict__:
            cls._the_instance = list.__new__(cls)
        return cls._the_instance

    def append(self, name):
        if name not in self:
            list.append(self, name)

    def extend(self, names):
        for name in names:
            if name in self:
                continue

            list.append(self, name)

    def remove(self, name):
        if name in self:
            list.remove(self, name)

    def clear(self):
        del self[:]

I was trying to make a singleton instance... but im guessing it doesn't work because lists are mutable. I know i could use a decorator but im stuck using 2.5 and not 2.6 =( ... Anyone have any ideas?

The singleton concept is intependent of mutability. You can have singleton mutable object. Also, decorators are part of the language since (IMO) 2.4.

I think your approach was principally correct, though I'd rather use an implementation like this (methods beyond construction skipped):

class MyList(list):
    __the_instance = None
    def __new__(cls, *elems):
        if cls.__the_instance is None:
            cls.__the_instance = list.__new__(cls)
        return cls.__the_instance
    def __init__(self, *elems):
        self.extend(elems)

def test():
    mylist1 = MyList(1, 2, 3) ; mylist2 = MyList()
    print mylist1, mylist2
    mylist1.append(4)
    del mylist1[1]
    print mylist1, mylist2

>>> test()
[1, 2, 3] [1, 2, 3]
[1, 3, 4] [1, 3, 4]

Edited 7 Years Ago by pythopian: n/a

Comments
Awesome post

Thanks a bunch man, thats exactally what i was looking for.

just wondering... can u explain what i did wrong, why my method does not work, and why yours does...

I just want to make sure i grasp this concept

just wondering... can u explain what i did wrong, why my method does not work, and why yours does...

I just want to make sure i grasp this concept

When I replace my __new__ implementation with yours, it works just the same (except that mine is a bit more efficient).

But then, you never said what didn't work in the first place...

Edited 7 Years Ago by pythopian: n/a

You can use this to generically "singletonize" virtually any class:

def Singleton(cls):
    instance = []
    def create(*args, **kw):
        if not instance:
            instance.append(cls(*args, **kw))
        return instance[0]
    return create

TheList = Singleton(list)

def test2():
    mylist1 = TheList([1,2,3])
    mylist2 = TheList()
    print mylist1, mylist2
    mylist1.append(4)
    del mylist1[1]
    print mylist1, mylist2

>>> test2()
[1, 2, 3] [1, 2, 3]
[1, 3, 4] [1, 3, 4]

Edited 7 Years Ago by pythopian: n/a

Sorry for the very slow reply, but for some reason this method is crashing my program... I guess i will have to try to figure out another solution :(

Sorry for the very slow reply, but for some reason this method is crashing my program... I guess i will have to try to figure out another solution :(

First, if it's crashing your program, there must be an exception traceback.
Also why don't you simply create a normal class and a single instance of the class ?

This question has already been answered. Start a new discussion instead.