类的特殊成员方法:
1. __doc__ #打印类的描述信息
class Dog(object):
"""this is document fot class Dog"""
def __init__(self):
pass;
"""this is document fot class Dog"""
def __init__(self):
pass;
print(Dog.__doc__)
输出:
this is document fot class Dog
2. __module__和__class__
__module__ : 表示当前操作的对象在哪个模块;
__class__: 表示当前操作的对象的类是什么;
libaa.py
class C(object):
def __init__(self):
self.name = "Brace"
from lib.aa import C
obj = C()
print(obj.__module__) #输出:lib.aa, 即:输出模块
print(obj.__class__) #输出:<class "lib.aa.C">, 即:输出类名
3. __init__和__del__
__init__: 构造方法,通过类创建对象时,自动触发执行
__del__: 析构方法,当对象在内存中被释放时,自动触发执行;
4. __call__:
对象后面再加括号,触发执行;
注:
构造方法的执行是由创造对象触发的,即:对象 = 类名();
而对于__call__方法的执行是有对象后加括号触发的,即对象()或者 类()()
class C(object):
def __init__(self, name):
self.name = name
def __call__(self, *args, **kwargs):
print("runing call ", args, kwargs)
d = C("Brace")
d(1,2,3,4, name="FFFF")
相当于:
C("Brace")(1,2,3,4, name="FFFF")
输出:
runing call (1, 2, 3, 4) {'name': 'FFFF'}
5. __dict__ 查看类或对象中的所有成员
class Dog(object):
def __init__(self, name):
self.name = name
def __call__(self, *args, **kwargs):
print("runing call ", args, kwargs)
def talk(self):
print("%s is talking...." % self.name)
print(Dog.__dict__) #打印类属性,不包括实例属性
d = Dog("Brace")
print(d.__dict__) #只打印实例属性,不包括类属性
输出:
{'__module__': '__main__', '__init__': <function Dog.__init__ at 0x0353BAE0>, '__call__': <function Dog.__call__ at 0x0353BA98>, 'talk': <function Dog.talk at 0x0353BA50>, '__dict__': <attribute '__dict__' of 'Dog' objects>, '__weakref__': <attribute '__weakref__' of 'Dog' objects>, '__doc__': None}
{'name': 'Brace'}
6. __str__
如果一个类中定义了__str__方法,那么在打印对象时,默认输出该方法的返回值;
7. __getitem__, __setitem__和__delitem__
用于索引操作,如字典,以上分别表示获取,设置,删除数据
输出:
this is document fot class Dog
2. __module__和__class__
__module__ : 表示当前操作的对象在哪个模块;
__class__: 表示当前操作的对象的类是什么;
libaa.py
class C(object):
def __init__(self):
self.name = "Brace"
from lib.aa import C
obj = C()
print(obj.__module__) #输出:lib.aa, 即:输出模块
print(obj.__class__) #输出:<class "lib.aa.C">, 即:输出类名
3. __init__和__del__
__init__: 构造方法,通过类创建对象时,自动触发执行
__del__: 析构方法,当对象在内存中被释放时,自动触发执行;
4. __call__:
对象后面再加括号,触发执行;
注:
构造方法的执行是由创造对象触发的,即:对象 = 类名();
而对于__call__方法的执行是有对象后加括号触发的,即对象()或者 类()()
class C(object):
def __init__(self, name):
self.name = name
def __call__(self, *args, **kwargs):
print("runing call ", args, kwargs)
d = C("Brace")
d(1,2,3,4, name="FFFF")
相当于:
C("Brace")(1,2,3,4, name="FFFF")
输出:
runing call (1, 2, 3, 4) {'name': 'FFFF'}
5. __dict__ 查看类或对象中的所有成员
class Dog(object):
def __init__(self, name):
self.name = name
def __call__(self, *args, **kwargs):
print("runing call ", args, kwargs)
def talk(self):
print("%s is talking...." % self.name)
print(Dog.__dict__) #打印类属性,不包括实例属性
d = Dog("Brace")
print(d.__dict__) #只打印实例属性,不包括类属性
输出:
{'__module__': '__main__', '__init__': <function Dog.__init__ at 0x0353BAE0>, '__call__': <function Dog.__call__ at 0x0353BA98>, 'talk': <function Dog.talk at 0x0353BA50>, '__dict__': <attribute '__dict__' of 'Dog' objects>, '__weakref__': <attribute '__weakref__' of 'Dog' objects>, '__doc__': None}
{'name': 'Brace'}
6. __str__
如果一个类中定义了__str__方法,那么在打印对象时,默认输出该方法的返回值;
7. __getitem__, __setitem__和__delitem__
用于索引操作,如字典,以上分别表示获取,设置,删除数据
8. __new__
1> __new__ 方法在实例化时也会执行,且会优先于__init__方法
2> __new__ 用来创建实例的,默认是自动执行,如果再写相当于重构了
3> return object.__new__(cls) 继承符类的__new__方法
def __new__(cls, *args, **kwargs):
print("--new---")
return object.__new__(cls) #必须要有返回值;object.__new__(cls)其实就是主类的内存对象
class Dog(object):
def __init__(self, name):
self.name = name
def talk(self):
print("%s is talking...." % self.name)
obj = Dog("Brace")
print(type(obj))
print(type(Dog))
输出:
<class '__main__.Dog'>
<class 'type'>
案例1:
def func(self):
print("Brace")
Foo = type("Foo", (object, ), {"talk": func})
print(type(Foo))
Foo.talk()
案例2:
def func(self):
print("the name is ", self.name)
def __init__(self, name, age):
self.name = name
self.age = age
Foo = type("Foo", (object, ), {"talk": func, "__init__": __init__})
print(type(Foo))
Foo("Brace", 26).talk()
输出:
<class 'type'>
the name is Brace
def __init__(self, name):
self.name = name
def talk(self):
print("%s is talking...." % self.name)
obj = Dog("Brace")
print(type(obj))
print(type(Dog))
输出:
<class '__main__.Dog'>
<class 'type'>
案例1:
def func(self):
print("Brace")
Foo = type("Foo", (object, ), {"talk": func})
print(type(Foo))
Foo.talk()
案例2:
def func(self):
print("the name is ", self.name)
def __init__(self, name, age):
self.name = name
self.age = age
Foo = type("Foo", (object, ), {"talk": func, "__init__": __init__})
print(type(Foo))
Foo("Brace", 26).talk()
输出:
<class 'type'>
the name is Brace