- 2009/02/27 17:41
- Python
ちょっと気になることがあったのでテスト。
引数として何が渡ってるのか気になって。
結果に関してはいろいろ思うところある感じです。
これを有効に使えたらクラス使うのがもっと楽になって随分楽にプログラミング出来るかな。
特に__call__あたりうまく使えたら結構楽しくなりそうな気が。
ソースコード————————————————————————–
class Klass(object):
def __init__(self, *args):
print '__init__'
print args
def fnc(self, *args):
return 'a'
def __del__(self, *args):
print '__del__'
print args
def __add__(self, *args):
print '__add__'
print args
def __or__(self, *args):
print '__or__'
print args
def __str__(self, *args):
print '__str__'
print args
return 'a'
def __call__(self, *args):
print '__call__'
print args
def __getattr__(self, *args):
print '__getattr__'
print args
def __setattr__(self, *args):
print '__setattr__'
print args
def __getitem__(self, *args):
print '__getitem__'
print args
def __setitem__(self, *args):
print '__setitem__'
print args
def __len__(self, *args):
print '__len__'
print args
return 1
def __cmp__(self, *args):
print '__cmp__'
print args
def __lt__(self, *args):
print '__lt__'
print args
def __gt__(self, *args):
print '__gt__'
print args
def __eq__(self, *args):
print '__eq__'
print args
def __radd__(self, *args):
print '__radd__'
print args
def __rmul__(self, *args):
print '__rmul__'
print args
def __iadd__(self, *args):
print '__iadd__'
print args
return self
def __idiv__(self, *args):
print '__idiv__'
print args
return self
def __iter__(self, *args):
print '__iter__'
print args
for a in 'edcba':
print '> %s' % a
yield a
if __name__ == '__main__':
c = Klass()
print '# test __add__'
c + 1
print '# test __or__'
c | 2
print '# test __str__'
str(c)
print '# test __call__'
c.fnc()
print '# test __getatgtr__'
c.val
print '# test __setatgtr__'
c.v = 3
print '# test __getitem 1__'
'a' in c
print '# test __getitem 2__'
c[0]
print '# test __getitem 3__'
c['b']
print '# test __setitem__'
c[1] = 'c'
print '# test __len__'
len(c)
print '# test __cmp__ 1'
c == 'd'
print '# test __cmp__ 2'
c < 3
print '# test __cmp__ 3'
c > 4
print '# test __radd__'
10 + c
print '# test __rmul__'
10 * c
print '# test __iadd__'
c += 5
print '# test __idiv__'
c /= 2
print '# test __iter__'
for i in c:
print '>> %s' % i
出力————————————————————————–
__init__
()
# test __add__
__add__
(1,)
# test __or__
__or__
(2,)
# test __str__
__str__
()
# test __call__
# test __getatgtr__
__getattr__
(‘val’,)
# test __setatgtr__
__setattr__
(‘v’, 3)
# test __getitem 1__
__iter__
()
> e
> d
> c
> b
> a
# test __getitem 2__
__getitem__
(0,)
# test __getitem 3__
__getitem__
(‘b’,)
# test __setitem__
__setitem__
(1, ‘c’)
# test __len__
__len__
()
# test __cmp__ 1
__eq__
(‘d’,)
# test __cmp__ 2
__lt__
(3,)
# test __cmp__ 3
__gt__
(4,)
# test __radd__
__radd__
(10,)
# test __rmul__
__rmul__
(10,)
# test __iadd__
__iadd__
(5,)
# test __idiv__
__idiv__
(2,)
# test __iter__
__iter__
()
> e
>> e
> d
>> d
> c
>> c
> b
>> b
> a
>> a
__del__
()
- Newer: ハッカーと画家読了。
- Older: T. Rowe Price “Ink”
Comments:1
- tai 2009/02/27
ちなみにこれは引数に何をとるのかを確認したくて実行したテストです。
Pythonのクラスシステム、もうちょい詳しく調べてみるべきか。全然理解せずに使ってたなと反省:P
Trackbacks:0
- Trackback URL for this entry
- http://blog.taikomatsu.com/2009/02/27/python%e7%89%b9%e6%ae%8a%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e5%90%8d/trackback/
- Listed below are links to weblogs that reference
- [Python]特殊メソッド名 from memlog