设为首页 收藏本站
查看: 733|回复: 0

[经验分享] python 魔法方法(一)

[复制链接]

尚未签到

发表于 2017-4-28 10:57:46 | 显示全部楼层 |阅读模式
  一、__init__和__del__
  __init__主要是实例初始化, __del__相当于析构器

#/usr/bin/env python
#-*- coding:utf-8 -*-
class test(object):
#实例初始化
def __init__(self):
print "call __init__"
#析构器
def __del__(self):
print "call __del__"
t=test()

call __init__
call __del__
  init在实例化的时候调用,del在对象销毁时自动调用,一般类中都默认带有__del__不需要自己声明
  二、  __new__和__init__

官网解释:

If __new__()  returns an instance of cls, then the new instance’s __init__() 

method will be invoked like __init__(self[, ...]), where self is the new instance

and the remaining arguments are the same as were passed to __new__().

如果__new__()返回cls的实例,则新实例的__init__()方法会像__init__(self[,...])调用,其中self是新实例,其他的参数和传给__new__()中的一样If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.



  如果 __new__()不返回实例,则__init__()不会被调用
  1、__new__ 是在__init__之前被调用的特殊方法,不返回实例


class A(object):
def __init__(self):
print "call __init__"
def __new__(self):
print "call __new__"
a=A()——

call __new__
  2、返回实例

class A(object):
def __init__(self,x=0):
print "call __init__"
self.x=x
def __new__(cls):
print "call __new__"
#返回cls的实例
return super(A,cls).__new__(cls)
a=A()

call __new__
call __init__
  2种方法刚好验证官网的解释,另外__init__只是用来将传入的参数初始化给对象,__new__很少用到,除非你希望能够控制对象的创建。
  三、__call__
  类中定义了__call__ ()方法,表明类可以作为方法被调用

class B(object):
#define __call__
def __call__(self,x=0):
print "call __call__()"
#value that the method returns
return x
#instance
b=B()
#invoked as a method
b()
print b(1)

call __call__()
call __call__()
1
  可以看出作为方法调用时获得的返回值为__call__()中返回的值。
  四、其他的一些基本的定制魔法方法

#/usr/bin/env python
#-*- coding:utf-8 -*-
#instance
class C(object):
#内建str()以及print语句
def __str__(self):
print "call __str__"
return "class C __str__"
#内建repr()和操作符
def __repr__(self):
print "call __repr__"
return "class C __repr__"
#内建unicode()
def __unicode__(self):
print "call __unicode__"
return "call C __unicode__"
#内建bool()
def __nozero__(self):
print "call __nozeto__"
#内建len()
def __len__(self):
print "call __len__"
return 1
c=C()
#首先 c 会调用__str__中的print
#print c 会答应__str__方法返回值
print c
#repr(c)的时候调用 __repr__方法中的print
#print repr(c)会打印__repr__方法的返回值
print repr(c)
#unicode(c)的时候调用 __unicode__方法中的print
#print unicode(c)会打印__unicode__方法的返回值
print unicode(c)
#len(c)的时候调用 __len__方法中的print
#print len(c)会打印__len__方法的返回值
print len(c)
#bool(c)的时候调用 __len__方法中的print
#print bool(c)如果len(c)为0则返回False,非0则为True
print bool(c)
  结果:

call __str__
class C __str__
call __repr__
class C __repr__
call __unicode__
call C __unicode__
call __len__
1
call __len__
True

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-370328-1-1.html 上篇帖子: python shelve 本地数据存储 下篇帖子: Python 学习入门(10)—— 时间
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表