Python 面向对象--类的特殊成员

一. __new____init__

__new__ : 创建对象

__init__ : 初始化对象

class Foo(object):

    def __init__(self):
        print("初始化对象")

    def __new__(cls, *args, **kwargs):
        print("创建对象")
        return object.__new__(cls)

obj = Foo()

# 以上代码执行结果是:
# 创建对象
# 初始化对象

由此可以看出, python面向对象中 创建对象 的过程是:
(1)执行 __new__ 方法, 最后的返回值才是实例化对象, 这一步是创建对象的过程.
(2)执行 __init__ 方法, 把创建好的对象进行初始化.

 

二. __call__

__call__ :  在对象后面接一个括号, 表示触发执行__call__方法

class Foo:
  def __call__(self, *args, **kwargs):
    print("正在执行__call__方法")

obj = Foo()    # 执行__new__方法, 执行__init__方法
obj()    # 执行__call__方法

# 以上代码执行结果:
# 正在执行__call__方法

 

补充: Python内置函数callable的用法

callable()

描述: callable() 函数用于检查一个对象是否是可调用的. 如果返回True, object可能调用成功也可能调用失败, 视情况而定. 但如果返回False, 调用对象ojbect绝对不会成功.简单来讲, 只要内部有__call__方法都返回 True.

语法: callable(object)

参数: object -- 对象

返回值: 若对象可被调用返回True, 否则返回False.

例1:

class A:
    def method(self):
        pass

print(callable(A))  # 类都是可调用的, 返回True
a = A()
print(callable(a))  # 对象a没有实现__call__方法,返回False

例2:

class B:
    def __call__(self):
        pass

print(callable(B))  # 类返回True
b = B()
print(callable(b))  # 对象b实现了__call__方法,返回True

 

三. __getitem__, __setitem__, __delitem__

用于索引操作, 与字典的操作方法类似. 以上三者分别表示获取、设置、删除数据.

class Foo:
    def __getitem__(self, key):
        print("正在调用__getitem__方法, 获取到的key是%s" % key)

    def __setitem__(self, key, value):
        print("正在调用__setitem__方法, 此时key是'%s', value是'%s'" % (key, value))

    def __delitem__(self, key):
        print("正在调用__delitem__方法, 要删除的key是%s" % key)

obj = Foo()

result = obj["王力宏"]    # 默认执行__getitem__方法
obj["汪峰"] = "乐坛半壁江山"    # 默认执行__setitem__方法
del obj["王力宏"]    # 默认执行__delitem__方法

# 以上代码执行结果:
# 正在调用__getitem__方法, 获取到的key是王力宏
# 正在调用__setitem__方法, 此时key是'汪峰', value是'乐坛半壁江山'
# 正在调用__delitem__方法, 要删除的key是王力宏

 

四. __str__

__str__ : 如果一个类中定义了__str__方法, 那么在打印对象时, 默认输出该方法的返回值.

class Foo:
    def __str__(self):
        return "我使用了__str__方法"

obj = Foo()
print(obj)

# 以上代码执行结果:
# 我使用了__str__方法

 

五. __dict__

__dict__ : 获取到类或对象中的所有成员.

class Foo:pass
print(Foo.__dict__)    # 获取类Foo中的所有成员

以上代码执行结果是:
{'__module__': '__main__', '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__doc__': None}

 

六. __iter__

__iter__用于迭代器,之所以列表、字典、元组可以进行for循环,是因为这些类型的内部定义了 __iter__ 方法.

现在用以下步骤来说明__iter__的作用:

第一步:

class Foo(object): pass
obj = Foo()
for i in obj: print(i)

# 执行结果:
# TypeError: 'Foo' object is not iterable
# 报错结果显示: 'Foo'类对象是不可迭代的

第二步:

class Foo(object):
    def __iter__(self):
        pass

obj = Foo()

for i in obj:
    print(i)

# 执行结果:
# TypeError: iter() returned non-iterator of type 'NoneType'
# 报错结果显示: iter方法没有返回一个迭代器

第三步:

class Foo(object):
    def __init__(self, iteration):
        self.iteration = iteration

    def __iter__(self):
        return iter(self.iteration)

obj = Foo([11, 22, 33])

for i in obj:
    print(i)

# 执行结果:
# 11
# 22
# 33

从以上步骤可以看出, for循环迭代的其实是 iter([11, 22, 33]), 所以执行流程可以变更为:

obj = iter([11, 22, 33])
for i in obj: print(i)

 

七. __class____module__

__class__ : 表示当前操作的对象的类是什么.
__module__ : 表示当前操作的对象在哪个模块.

class Foo:pass

obj = Foo()
print(obj.__class__)    # 当前操作的对象的类是Foo
print(obj.__module__)   # 当前操作的对象在__main__模块中

# 以上代码执行结果:
# <class '__main__.Foo'>
# __main__

---------------------------------------

参考资料: python面向对象(三) 特殊成员

---------------------------------------

 

posted @ 2018-10-06 16:54  咕噜噜~  阅读(493)  评论(0编辑  收藏  举报