![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,784
Rep Power: 5
![]() |
More Abstract Use Of Class Methods
I'm trying to achieve a simple solution to requiring to pack a value alongside an integer, so it can be easily remembered at a later time.
class keep_index:
def __init__(self, value, index):
self.value = str(value)
self.index = index
def __call__(self):
return self.index
def __str__(self):
return self.value
x = keep_index(4, 1)
print x
print x()As you can see, this works. Printing x, prints out 4. But calling it returns a value I added alongside it. However, it is a string in my example, not an integer. I have tried __int__ instead of __str__ of course. That was my first attempt, but that doesn't work. Since I got this far by trial and error by dir()'ing some of the classes like "int" and "str", I may be missing something blatantly obvious. Sorry to be a pain. Another problem, is if I put 'x' inside a list, and print the list, it displays the function address rather then the value of ".value". And I'd rather not change the concept behind how this is working. I believe that packing an "invisible" value alongside the original would work beautifully. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,784
Rep Power: 5
![]() |
Some progress has been made. Changing __str__ to __repr__ allowed it to appear properly inside lists. However, it's still a string and not an integer (best case scenario might even be boolean).
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
This seems an odd request - would it not be more readable to refer to the index by obj.index instead of obj()?
Regardless, if I'm understanding you right, you can get the functionality you want by subclassing the int type: class keep_index(int):
def __new__(cls, value, index):
obj = int.__new__(cls, value)
obj.index = index
return obj
def __call__(self):
return self.index |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,784
Rep Power: 5
![]() |
That's excellent. Thanks. So does that work by inheriting the class "int"? I've only worked with class inheritance for wxPython and it looks like fun. Do you have any quick programming challenges you can think of that involve making inherited classes?
Your solution also solved the problem that it wasn't evaluating boolean expressions properly. I had to add more methods like __gt__, __ge__, to get it to work properly before. |
|
|
|
|
|
#5 | ||
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Quote:
Quote:
mylist = advanced_list(1, 2, 3, 4, 5) print mylist.len() print mylist.filter(lambda x : (x % 2) == 0) print mylist.map(lambda x: x * 2) |
||
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,784
Rep Power: 5
![]() |
Thanks a bunch.
![]() class advanced_list(list):
def __init__(cls, *args):
setattr(cls, 'len', cls._len)
setattr(cls, 'filter', cls._filter)
setattr(cls, 'map', cls._map)
obj = list.__init__(cls, list(args))
return obj
def _len(self):
return len(self)
def _filter(self, func):
return filter(func, self)
def _map(self, func):
return map(func, self)
mylist = advanced_list(1, 2, 3, 4, 5)
print mylist.len()
print mylist.filter(lambda x : (x % 2) == 0)
print mylist.map(lambda x: x * 2) |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Pretty good
- Although you don't have to do the "return obj" stuff. That's only for __new__, not __init__. The difference between __new__ and __init__ is subtle, but worth knowing.Take the following object creation code (note that Foobar has to be a new-style class) : obj = Foobar(1, 2) obj = Foobar.__new__(Foobar, 1, 2) obj.__init__(1, 2) The __init__ method is the initialiser. It is called after the object is constructed, and is used to give the object its initial data. The __init__ method's return value is ignored. Returning back to your code, it can be simplified somewhat: class advanced_list(list): def __init__(self, *args): list.__init__(self, args) def len(self): return len(self) def filter(self, func): return filter(func, self) def map(self, func): return map(func, self) |
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,784
Rep Power: 5
![]() |
I did the setattr because it's a bad habit to use function names that are reserved.
|
|
|
|
|
|
#9 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#10 | |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|