关于python中__new__的用法
来源:互联网 发布:手机能开通淘宝店吗 编辑:程序博客网 时间:2024/06/06 10:06
遇到一个问题:
要求实现以下结果:
a = A(1, 'name')print a == 1 #Trueprint a.name == 'name' #True
也就是构造一个A类,使得实例的值等于int类型的1,实例的name属性为字符串类型的'name'。
可以用__new__这个魔术方法解决,它会在__init__之前调用,构造对象,且返回值.
class A(int): def __new__(cls, id, name): obj = int.__new__(cls, id) obj.name = name return obj
a = A(1, 'name')print a == 1print a.name == 'name'
- 关于python中__new__的用法
- python中__new__ 的作用
- python中__new__和__init__的区别
- [Python] 新式类中 __new__的理解
- Python中__new__方法的使用
- Python的__new__方法
- 关于python中的__new__方法
- Python中__new__() 和 __init__()
- python中__new__和__init__
- Python中__init__和__new__的区别详解
- Python中__new__()方法的使用和实例化
- python中__init__()方法和__new__()方法的区别
- python __init__和__new__之间的区别
- python __init__和__new__之间的区别
- python之__init__()和__new__()的区分
- python __init__和__new__之间的区别
- Python---类的"__new__"和"__init__"方法
- Python中的__new__和__init__的区别
- java 多态实例
- Solaris/Linux 命令行 简单手册
- WPF之路——绘制几何图形
- SecurityContextPersistenceFilter
- 8-数据库管理
- 关于python中__new__的用法
- Android OnLowMemory和OnTrimMemory
- alsa音频架构1
- 【四圣龙神录的编程教室】第10章、把敌人表示出来看看吧
- ThinkPHP URL路由解析
- apue 1 课后习题
- CodeForces 366 E. Dima and Magic Guitar
- poj 1177 picture 线段树
- 推荐Linux新手应该访问的5个国外网站: