Python Foundation - part three
2015-08-19 02:48
513 查看
Metaclasses in Python3
>>> class FooBar: pass >>> type(FooBar) <class 'type'> >>> f = FooBar() >>> type(f) <class '__main__.FooBar'>
>>> isinstance(foo, Foobar) True >>> isinstance(Foobar, type) True
These examples mean instance is an instance of a class, a class is an instance of metaclass. and
typeis very useful metaclass in python as we have seen.
we can use
typeto create a class like this:
>>> MyClass = type('MyClass', (), {}) >>> MyClass <class '__main__.MyClass'>
As type is a metaclass, so we can also create a custom metaclass that extens type
>>> class Meta(type): ... pass
Difference between , _ and xx in Python
One underscore in the beginning
one underscore marks a private method or attributeTwo underscores in the beginning
Such methods with two underscores can not be overrided.Two underscores in the beginning and in the end
Such methods are called magic methods which only python can call. Some useful methods are__call__
__new__
__init__
__prepare__
These four methods are very important for instance and class creation.
相关文章推荐
- python-关键字参数与非关键字参数(可变参数)详解
- python-装饰器
- (转载)Python中:self和__init__的含义 +
- Python入门(十四) 字符串
- Python中字典的基础知识归纳小结
- 详解Python中映射类型(字典)操作符的概念和使用
- 详解Python中映射类型的内建函数和工厂函数
- 深入理解Python中字典的键的使用
- Python中的集合类型知识讲解
- 深入解析Python中的集合类型操作符
- Python中集合的内建函数和内建方法学习教程
- 关于Panda3D version 1.81版本遇到的问题
- Python格式化日期
- 【转载】在Ubuntu下配置舒服的Python开发环境
- Instant Python 中文缩减版
- 好久没来了,重出江湖,共享个python34+pyqt+pyserial串口工具源码
- Python Foundation - part two
- Python解LeetCode 131: Palindrome Partitioning(Python中的浅复制与深复制)
- python科学计算_numpy_线性代数/掩码数组/内存映射数组
- Python学习(一)初识