2008年11月6日 星期四

object & return

Python 的 class 功能真的比我想像中的強大多了 ...

這是一個範例,它定義了 object 各種需求的 return function
class a:
    
def __int__(self):
        
return 123456789
    
    
def __str__(self):
        
return 'this is a string'
    
    
def __unicode__(self):
        
return 'unicode here'



使用起來會像這樣
>>> s = a()
>>> int(s)
123456789
>>> str(s)
'this is a string'
>>> unicode(s)
u'unicode here'



可以應用在蠻多場合的!

沒有留言: