>>> class userdict(dict):
... def keys(self): return 'userdict'
...
>>> d = userdict(zip(['a', 'b', 'c'], [1, 2, 3]))
>>> print d
{'a': 1, 'c': 3, 'b': 2}
>>> d['b']
2
>>> d.keys()
'userdict'
>>> type(d)
class '__main__.userdict'
补充(2006-10-03):
>>> isinstance(d, userdict)
True
>>> help(isinstance)
>>> imoprt __builtin__
>>> help(__builtin__.isinstance)
isinstance(...)
isinstance(object, class-or-type-or-tuple) -> bool
>>> type(userdict)
type 'type'
>>> class user:
... def func(self): return 'user'
...
>>> type(user)
type 'classobj'
>>> type(int)
type 'type'
>>> type(type)
type 'type'
>>> type(object)
type 'type'
>>> issubclass(userdict, object)
True
>>> issubclass(int, object)
True
>>> help(list.__class__)
Help on class type in module __builtin__:
class type(object)
>>> help(dict.__class__)
# 同上
>>> type(class) # HOW TO ?!
星期四, 九月 28, 2006
从 userdict 看 python 型与类及其继承
订阅:
博文评论 (Atom)
没有评论:
发表评论