#单例(单个实例)模式,永远使用同一份实例(对象)

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:Singleton_mode.py
# time:2021/03/05

class Foo:
    def __init__(self,name,age):
        self.name = name
        self.age = age

obj = Foo('smoke',18)    # obj对象,obj也称为Foo类的实例(实例化)
obj1 = Foo('smoke',18)
obj2 = Foo('smoke',18)
obj3 = Foo('smoke',18)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/Singleton_mode.py

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:Singleton_mode.py
# time:2021/03/05

class Foo:
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def show(self):
        print(self.name,self.age)

v = None
while True:
    if v:
        v.show()
    else:
        v = Foo('smoke',123)
        v.show()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/Singleton_mode.py
smoke 123
smoke 123
smoke 123
smoke 123
smoke 123
smoke 123
smoke 123
smoke 123
smoke 123

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:Singleton_mode.py
# time:2021/03/05

class Foo:
    __v = None

    @classmethod
    def get_instance(cls):
        if cls.__v:
            return cls.__v
        else:
            cls.__v = Foo()
            return cls.__v

# 不要在使用 类()
obj = Foo.get_instance()
print(obj)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/Singleton_mode.py
<__main__.Foo object at 0x7ffa08a20580>

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:Singleton_mode.py
# time:2021/03/05

class Foo:
    __v = None

    @classmethod
    def get_instance(cls):
        if cls.__v:
            return cls.__v
        else:
            cls.__v = Foo()
            return cls.__v

# # 不要在使用 类()
obj1 = Foo.get_instance()
print(obj1)
obj2 = Foo.get_instance()
print(obj2)
obj3 = Foo.get_instance()
print(obj3)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/Singleton_mode.py
<__main__.Foo object at 0x7f6ce3d1f580>
<__main__.Foo object at 0x7f6ce3d1f580>
<__main__.Foo object at 0x7f6ce3d1f580>

Process finished with exit code 0

安装tornado(web框架)

smoke@smoke-GS70-2PC-Stealth:~$ sudo apt install python3-pip
smoke@smoke-GS70-2PC-Stealth:~$ sudo pip3 install tornado

web服务器代码

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:Singleton_mode.py
# time:2021/03/05

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write('hello, world')

application = tornado.web.Application ([
    (r"/index", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/Singleton_mode.py

通过浏览器访问http://127.0.0.1:8888/index

 

修改代码

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:Singleton_mode.py
# time:2021/03/05

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write('hello, world123123')

application = tornado.web.Application ([
    (r"/index", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/Singleton_mode.py

通过浏览器访问http://127.0.0.1:8888/index

 

 修改代码

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:Singleton_mode.py
# time:2021/03/05

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        import time

        self.write(str(time.time()))

application = tornado.web.Application ([
    (r"/index", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/Singleton_mode.py

通过浏览器访问http://127.0.0.1:8888/index

作业:

选课系统

角色:学校、学院、课程、讲师

要求

1.创建北京、上海2所学校

2.创建linux,python,go 3个歌城,linux\py在北京开,go在上海开

3.课程包含,周期,价格,通过学校创建课程

4.通过学校创建班级,班级关联课程、讲师

5.创建学院时,选择学校,关联班级

6.创建讲师角色时要关联学校,

6.1学院视图,可以注册,交学费,选择班级,

6.2讲师视图,讲师可管理自己班级,上课时选择班级,查看班级学院列表,修改所有管理学员的成绩。

6.3管理视图,创建讲师,创建半级,创建课程

7.上面所有操作产生的数据都通过pickle序列化保存到文件里

类的封装嵌套

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:encapsulation.py
# time:2021/03/06

class Tearch:
    def __init__(self,name,age):
        self.name = name
        self.age = age

class Course:
    def __init__(self,name,tearcher):
        self.name = name
        self.tearcher = tearcher

t1 = Tearch('李杰',8)
t2 = Tearch('烧饼',9)
t3 = Tearch('豆饼',10)

c1 = Course('生理课',t1)
print(c1.name)
print(c1.tearcher.age)
print(c1.tearcher.name)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/encapsulation.py
生理课
8
李杰

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:encapsulation.py
# time:2021/03/06

class Tearch:
    def __init__(self,name,age):
        self.name = name
        self.age = age
        self.salary = 1000

class Course:
    def __init__(self,name,cost,tearcher):
        self.name = name
        self.tearcher = tearcher
        self.cost = cost

    def class_up(self):
        self.tearcher.salary += self.cost
        self.cost

t1 = Tearch('李杰',8)
t2 = Tearch('烧饼',9)
t3 = Tearch('豆饼',10)

c1 = Course('生理课',1,t1)
print(c1.name)
print(c1.tearcher.age)
print(c1.tearcher.name)
print(c1.tearcher.salary)
c1.class_up()
print(c1.tearcher.salary)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/encapsulation.py
生理课
8
李杰
1000
1001

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:encapsulation.py
# time:2021/03/06

class F1:
    def __init__(self):
        self.name = 123

class F2:
    def __init__(self,a):
        self.ff = a

class F3:
    def __init__(self,b):
        self.dd = b

f1 = F1()    #[name=123]
f2 = F2(f1)    #[ff=[name=123]]
f3 = F3(f2)    #[dd=[ff=[name=123]]]
# 找到123并输出
print(f3.dd.ff.name)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/encapsulation.py
123

Process finished with exit code 0