Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Jun 2nd, 2006, 8:27 PM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,825
Rep Power: 5 Sane will become famous soon enough
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.
Sane is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:15 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC