python - 面向对象

面向对象概念:

特性: class object
封装
集成
多态

===

可以给python的对象添加属性。

class Riole:

    sex = 'male'

    def __init__(self, name):

        self.name = name
    def shot(self, who):
        print "shot %s" %who

r1 = Riole('a')

r1.ax = 'asd'

print r1.ax

print r1.sex

类变量的用途,是大家共有的属性,节省开销。

可以改变对象的类变量,其实是初始化对象时候在内存新给了一个内存地址存放”类变量“,若果类变量是集合形式(列表,字典)类变量是共用的:

class Riole:

    sex = 'male'

    def __init__(self, name):

        self.name = name
    def shot(self, who):
        print "shot %s" %who

r1 = Riole('a')

r2 = Riole('b')

r1.ax = 'asd'

print r1.ax

r1.sex = 'female'

print r1.sex
print r2.sex

print Riole.sex

这个例子不一样:

class Rio:

    num = 0
    num_list = []
    def __init__(self, name, age):
        self.name = name
        self.age = age

r1 = Rio('a', 1)
r2 = Rio('b', 1)
r3 = Rio('c', 1)

r1.num = 22
r1.num_list.append('a')


r4 = Rio('d', 1)

print r2.num_list
print r4.num_list

构造函数与析构函数:

图片.png

析构函数:在实例释放,销毁的时候执行的,通常用于做一些收尾工作。比如关闭一些数据库链接。

class Person:
    cn = 'China'
    def __init__(self,name, age, address):
        self.name = name
        self.age = age
        self.address = address
    def __del__(self):
        print 'del self'

析构函数不用传递参数。


在子类新加实例属性:

class People:

    def __init__(self, name, age):
        self.name = name
        self.age = age
    def eat(self):
        print("%s is eating"  %self.name)

    def sleep(self):
        print("%s is sleeping "  %self.name )


class Man(People):

    def __init__(self, name, age, money):
        #super().__init__(name, age)   # or People.__init__(self, name, age)  # or super(Man, self).__init__(name, age)
        People.__init__(self, name, age)
        self.money = money

    def piao(self):
        print("%s is piaoing" %self.name)
    def sleep(self):
        People.sleep(self)
        print("%s is sleeping very good." %self.name)

注意:上面的:

 def sleep(self):
        People.sleep(self)
        print("%s is sleeping very good." %self.name)

People.sleep(self)调用了父类的sleep方法,注意调用方式。
等价于super().sleep()


经典类与新式类

以后都统一使用新式类的写法,这个是规范。

class Person:    # 经典类

class Person(object ):    # 新式类

Python 2.x中默认都是经典类,只有显式继承了object才是新式类
Python 3.x中默认都是新式类,不必显式的继承object

object是基类,所有类的基类。

class SchoolMember(object):

=====

类方法:

class Animal(object):
    def __init__(self, name):
        self.name = name

    def talk(self):
        pass
    @classmethod
    def animal_talk(self, obj):
        obj.talk()

class Dog(Animal):
    def talk(self):
        print("往往")

class Cat(Animal):
    def talk(self):
        print("喵喵")

d = Dog("dog")
c = Cat("cat")

ani = Animal('animal')

Animal.animal_talk(c)

=====

pickle是可以序列化任意对象的。

====

1._function与function:


def default_quota_update(request, **kwargs):
    novaclient(request).quota_classes.update(DEFAULT_QUOTA_NAME, **kwargs)

def _get_usage_marker(usage):
    marker = None
    if hasattr(usage, 'server_usages') and usage.server_usages:
        marker = usage.server_usages[-1].get('instance_id')
    return marker

带下划线的函数,是私有函数,使用import的时候,导入不进去。

推荐阅读更多精彩内容