subclassing 'list'

A

akineko

Hello everyone,

I'm creating a class which is subclassed from list (Bulit-in type).

It works great.
However, I'm having a hard time finding a way to set a new value to
the object (within the class).
There are methods that alter a part of the object (ex. __setitem__()).
But I couldn't find any method that can replace the value of the
object.
I wanted to do something like the following:

class Mylist(list):

def arrange(self):
new_value = ....
list.self.__assign__.(self, new_value)

I serached the newsgroup and found that assignment operator ('=')
cannot be overridden because it is not an operator. But it shouldn't
stop Python to provide a way to re-assign the value internally.

Any suggestions will be highly appreciated.

Best regards,
Aki-
 
C

Chris Rebert

Hello everyone,

I'm creating a class which is subclassed from list (Bulit-in type).

It works great.
However, I'm having a hard time finding a way to set a new value to
the object (within the class).
There are methods that alter a part of the object (ex. __setitem__()).
But I couldn't find any method that can replace the value of the
object.
I wanted to do something like the following:

class Mylist(list):

def arrange(self):
new_value = ....
list.self.__assign__.(self, new_value)

If you mean you want to replace the contents of the list with that of
another list, just do:

def arrange(self):
self.clear() #empty the list
self.extend(new_list) #append the contents of other list


If you instead mean that you want the object to somehow completely
"become" or be replaced by another object, that's not possible (short
of proxying). Smalltalk has a `become` method, but not Python (it's
pretty deep black magic anyway). If you gave more info about _why_ you
want to do that, someone could probably suggest an alternative.

Cheers,
Chris
 
J

James Stroud

akineko said:
Hello everyone,

I'm creating a class which is subclassed from list (Bulit-in type).

It works great.
However, I'm having a hard time finding a way to set a new value to
the object (within the class).
There are methods that alter a part of the object (ex. __setitem__()).
But I couldn't find any method that can replace the value of the
object.
I wanted to do something like the following:

class Mylist(list):

def arrange(self):
new_value = ....
list.self.__assign__.(self, new_value)

I serached the newsgroup and found that assignment operator ('=')
cannot be overridden because it is not an operator. But it shouldn't
stop Python to provide a way to re-assign the value internally.

Any suggestions will be highly appreciated.

Best regards,
Aki-

Slice assignment will behave as you like:

py> class Mylist(list):
.... def arrange(self):
.... new_value = [1, 2, 3]
.... self[:] = new_value
....
py> m = Mylist()
py> m.arrange()
py> m
[1, 2, 3]

Your other option is to arrange the list in-place, depending on what it
means to arrange the list. You may want to provide a more explicit example.
 
A

akineko

Hello Chris and James,

Thank you for you guys' prompt response.
Yes, that is what I wanted to do.
I, somehow, didn't think of using those list methods.
Instead, I was looking for a single method to override.

Big Thanks!
Aki-


If you mean you want to replace the contents of the list with that of
another list, just do:

def arrange(self):
self.clear() #empty the list
self.extend(new_list) #append the contents of other list

Slice assignment will behave as you like:

py> class Mylist(list):
... def arrange(self):
... new_value = [1, 2, 3]
... self[:] = new_value
...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top