python类进阶2_20160709

多态

区分多态(同一对象对于不同对象作出不同的反应)和重载(对象功能改变,适应新的情况)

封装与私有化

__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__方法前调用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,219评论 18 399
  • 教程地址:python进阶 - 慕课网 python函数式编程 变量可以指向函数: f = absprint(f)...
    竹口小生阅读 3,704评论 0 2
  • 1.import static是Java 5增加的功能,就是将Import类中的静态方法,可以作为本类的静态方法来...
    XLsn0w阅读 5,028评论 0 2
  • 面向对象主要针对面向过程。 面向过程的基本单元是函数。 什么是对象:EVERYTHING IS OBJECT(万物...
    sinpi阅读 4,783评论 0 4
  • 9-22继续持有安记食品
    人生亦匆匆阅读 1,423评论 0 0