在大多数语言中, 类只是用来创建对象,但在python中,类也是对象
class ObjectCreator(object):
pass
my_object = ObjectCreator()
print my_object
#<__main__.ObjectCreator object at 0x017F21B0>
print ObjectCreator
#<class '__main__.ObjectCreator'>
因此,你能做如下事:
- 赋予变量
a = ObjectCreator print a
- 增加属性
print hasattr(ObjectCreator, 'new_attr') ObjectCreator.new_attr = 'foo' print hasattr(ObjectCreator, 'new_attr') print ObjectCreator.new_attr
- 作为函数的参数
def echo(o): print(o) echo(ObjectCreator)
- 拷贝
既然类是对象, 你可以动态创建类
class FooChild(Foo):
bar = True
Foo = type('FooChild', (Foo,), {'bar':True})
def echo_bar(self):
print self.bar
FooChild_2 = type('Foo', (Foo,), {'echo_bar'}:echo_bar)
type的语法:
type(name of the class,
tuple of the parent class (for inheritance, can be empty),
dictionary containing attributes names and values)
Python中下划线
以单个下划线开头(_foo)的代表不能直接访问的类属性,只有类对象和子类对象自己能访问到这些变量,需通过类提供的接口进行访问,不能用“from xxx import *”而导入;
以双下划线开头的(__foo)代表类的私有成员,意思是只有类对象自己能访问,连子类对象也不能访问到这个数据;
以双下划线开头和结尾的(__foo__)代表python里特殊方法专用的标识,如 __init__()代表类的构造函数。
Super
super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种问题。总之前人留下的经验就是:保持一致性。要不全部用类名调用父类,要不就全部用 super,不要一半一半。
注意super继承只能用于新式类,用于经典类时就会报错。
新式类:必须有继承的类,如果没什么想继承的,那就继承object
经典类:没有父类,如果此时调用super就会出现错误:『super() argument 1 must be type, not classobj』
新式类:必须有继承的类,如果没什么想继承的,那就继承object
经典类:没有父类,如果此时调用super就会出现错误:『super() argument 1 must be type, not classobj』
<span style="font-family:Microsoft YaHei;font-size:12px;"><span style="font-family:Microsoft YaHei;">class FooParent(object):
def __init__(self):
self.parent = 'I\'m the parent.'
print 'Parent'
def bar(self,message):
print message, 'from Parent'
class FooChild(FooParent):
def __init__(self):
FooParent.__init__(self)
print 'Child'
def bar(self,message):
FooParent.bar(self,message)
print 'Child bar function.'
print self.parent
if __name__=='__main__':
fooChild = FooChild()
fooChild.bar('HelloWorld')
-----------------------------------
class FooParent(object):
def __init__(self):
self.parent = 'I\'m the parent.'
print 'Parent'
def bar(self,message):
print message,'from Parent'
class FooChild(FooParent):
def __init__(self):
super(FooChild,self).__init__()
print 'Child'
def bar(self,message):
super(FooChild, self).bar(message)
print 'Child bar fuction'
print self.parent
if __name__ == '__main__':
fooChild = FooChild()
fooChild.bar('HelloWorld')
--------------------------------
#程序运行结果相同,为:
#Parent
#Child
#HelloWorld from Parent
#Child bar fuction
#I'm the parent.
</span></span>
Object
python 2 中没有什么继承, 就继承object(继承object用的是新式类)
<span style="font-family:Microsoft YaHei;font-size:12px;"><span style="font-family:Microsoft YaHei;">class Foo(object):
pass
class Foo1:
pass
print type(Foo), type(Foo1)
print dir(Foo)
print dir(Foo1)
#结果如下:
<type 'type'> <type 'classobj'>
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
['__doc__', '__module__']</span></span>