多态
区分多态(同一对象对于不同对象作出不同的反应)和重载(对象功能改变,适应新的情况)
封装与私有化
__name 就是私有的
_name 被隐藏了(?)
特殊类属性
>>> class F(object):
'''
this is my class
'''
def __init__(self):
self.lang = 'python'
def get_lang(self):
print (self.lang)
>>> F.__name__ #类名称
'F'
>>> F.__doc__#类文档
'\n\tthis is my class\n\t'
>>> F.__bases__#类的子类
(<class 'object'>,)
>>> class C(F):
pass
>>> C.__bases__
(<class '__main__.F'>,)
>>> F.__dict__#类的属性
mappingproxy({'__doc__': '\n\tthis is my class\n\t', 'get_lang': <function F.get_lang at 0x0396A858>, '__module__': '__main__', '__dict__': <attribute '__dict__' of 'F' objects>, '__weakref__': <attribute '__weakref__' of 'F' objects>, '__init__': <function F.__init__ at 0x0396A8A0>})
>>> F.__module__#类所属的模块
'__main__'
>>> F.__class__#类的类型
<class 'type'>
>>> str.__class__
<class 'type'>
>>> str.__name__
'str'
>>> str.__doc__
"str(object='') -> str\nstr(bytes_or_buffer[, encoding[, errors]]) -> str\n\nCreate a new string object from the given object. If encoding or\nerrors is specified, then the object must expose a data buffer\nthat will be decoded using the given encoding and error handler.\nOtherwise, returns the result of object.__str__() (if defined)\nor repr(object).\nencoding defaults to sys.getdefaultencoding().\nerrors defaults to 'strict'."
实例也可以再定义属性
>>> f = F()
>>> f.name = 'canglaoshi'
>>> f.__dict__
{'lang': 'python', 'name': 'canglaoshi'}
关于__new__
和__init__
方法
__new__需要return __init__不要return __new__方法,在__init__方法前调用