`
rockkyle
  • 浏览: 10358 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

python 魔法方法(一)

 
阅读更多

一、__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

 

分享到:
评论

相关推荐

    用Python画出一个魔法阵

    用Python画出一个魔法阵 turtle

    Python魔法方法详解

    他们是可以给你的类增加魔力的特殊方法,如果你的对象实现(重载)了这些方法中的某一个,那么这个方法就会在特殊的情况下被 Python 所调用,你可以定义自己想要的行为,而这一切都是自动发生的。 Python 的魔术方法...

    Python黑魔法指南v3.01

    第一章:魔法冷知识最后,这东西是一个单例。那这东西有啥用呢?1. 它是 Numpy 的一个语法糖...$ python3 demo.py1.2 使用 end 来

    Python黑魔法手册 2.0 文档.pdf

    Python黑魔法手册 2.0 文档.pdf

    Python 黑魔法指南 v3.0(1).tmp

    Python 黑魔法指南 v3.0(1)

    Python魔法方法功能与用法简介

    本文实例讲述了Python魔法方法功能与用法。分享给大家供大家参考,具体如下: 1、什么是魔法方法? 魔法方法就是可以给你的类增加魔力的特殊方法,如果你的对象实现(重载)了这些方法中的某一个,那么这个方法就会...

    通俗易懂的python魔法方法

    python的魔法方法类似于C++中的重载,在python中采用双下划线包围函数名的方法来实现对已有函数的重写,实现开发者想要的功能。python的魔法方法很多,这里直接可以参考https://fishc.com.cn/thread-48793-1-2.html...

    Python 黑魔法指南 v1.0.zip

    Python 黑魔法指南,里边有关于python使用的小技巧,帮助你快速上手python

    python魔法方法-自定义序列详解

    例如,如果要将一个类要实现迭代,就必须实现两个魔法方法:__iter__、next(python3.x中为__new__)。__iter__应该返回一个对象,这个对象必须实现 next 方法,通常返回的是 self 本身。而 next 方法必须在每次调用...

    Python-Python黑魔法实现集锦

    Python“黑魔法”实现集锦

    Python魔法方法 容器部方法详解

    这篇文章主要介绍了Python魔法方法 容器部方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 为了加深印象,也为了以后能够更好的回忆,还是记录一下。 ...

    ヴィッキーサクラの魔法陣(PYTHON)2.0-COPY.zip

    ヴィッキーサクラの魔法陣(PYTHON)2.0-COPY.zip Key:NULL 看起来VickySakura(わたし/私)的魔法阵更漂亮了 在Python2.7.18上可以使用~

    Python 黑魔法指南 v1.0

    书名叫做《Python 黑魔法手册》,该手册的作者(明哥)是一个从事云计算多年的 Python 重度用户,它把自已多年的 Python 编码经验整理成小册子,没有长篇大论,半天就能全能掌握,让你一天就能收获别人一年的技能及...

    Python黑魔法-异步IO.pdf

    Python黑魔法-异步IO.pdf

    python 魔法函数实例及解析

    python的几个魔法函数 __repr__ Python中这个__repr__函数,对应repr(object)这个函数,返回一个可以用来表示对象的可打印字符串.如果我们直接打印一个类,向下面这样 class A(): def __init__(self,name=None...

Global site tag (gtag.js) - Google Analytics