zoukankan      html  css  js  c++  java
  • 元类, pymysql

    元类, pymysql

    一、元类

    • 自定义元类
    '''
    1、什么是元类?
        - 类的类就是type,其实type就是元类
    
    2、元类的作用?
    
    3、如何创建元类以及使用?
    '''
    
    # # 1、一切皆对象
    # list1 = []  # list1 = list([])
    # print(type(list1))  #<class 'list'>
    
    #
    # # # 2、自定义一个类
    # class Chinese(object):
    #     country = 'china'
    #
    #     def __init__(self, name, age, sex):
    #         self.name = name
    #         self.age = age
    #         self.sex = sex
    #
    #
    # obj = Chinese('yafeng', 18, 'male')
    # print(type(obj))   #<class '__main__.Chinese'>
    # print(Chinese)    #类本质上也是一个对象,Python中一且皆对象
    # # <class '__main__.Chinese'>,
    
    
    # 3、如何产生类
    #1) 通过class关键字产生类
    #2) 通过调用type类:type()  ----> obj ---->Chinese
    
    
    # what:指的是类名
    # bases:继承的父类(object)
    # dict:类的名称空间
    
    #
    # code = '''
    # country = 'china'
    # def __init__(self, name, age, sex):
    #     self.name = name
    #     self.age = age
    #     self.sex = sex
    # '''
    
    # class_attr = {}
    # exec(code, {}, class_attr)
    #
    #
    # # type(类的名字, 类的基类,类的名称空间)
    # obj = type('Chinese', (object, ), class_attr)
    # print(obj)  #<class '__main__.Chinese'>
    # print(Chinese)
    
    #1、什么是元类?
    # 类的类就是type,其实type就是元类
    
    
    # 2、为什么要使用元类?
    # 因为元类可以控制类的创建过程
    
    #type是python内置的元类
    
    
    # 自定义一个元类
    class MyMetaClass(type):
    
    
        #控制类的创建
        # 优酷需要使用的部分
        def __init__(self, class_name, class_bases, class_dict):
            print(type(class_name))
    
            #要求类的首字母必须大写
            if not class_name.istitle():
                raise NameError('类的首字母必须大写!')
    
            # 规定类必须要写注释
            if not class_dict.get('__doc__'):
                raise TypeError('必须得写注释!!!')
    
            # 必须将类的类名,类的基类,类的名称空间一并返给 type 中的__init__
            super().__init__(class_name, class_bases, class_dict)
    
    
    
        # 了解:元类更深层次的作用
        # 控制调用类的行为
        # 为什么调用类就一定会产生一个空对象,为什么一定会执行__new__
        # 其实就是type 内部一定会调用一次__call__,有__call__来帮你调用__new__
    
        # 元类中的__call__就是创建类的过程!!!
        def __call__(self, *args, **kwargs):
            print(args)   # User类括号中的值
            # 1、造一个空对象obj
            obj = object.__new__(self) # 创造一个空对象 self ---> User
    
            print(obj.__dict__, 1111111)
    
            # 2、调用类时,__call__会立马调用User.__init__, 并且将obj连同User括号内的参数一同传给__init__
            self.__init__(obj, *args, **kwargs)
    
            # return 一个真正创建的对象
            return obj
    
    
    ## obj = MyMetaClass()
    # obj()   # obj()---->  User(10,20) ---->user_obj
    
    
    # 被控制类在定义阶段 类名(metaclass=自定义的元类)---->会将当前类的类名、基类、类的名称空间  一并传给 自定义的元类
    
    # metaclass --->自定义的元类看---->低调做那个自定义的元类(类名,基类,类的名称空间)
    
    # type(类名,基类,类的名称空间)
    
    class User(object, metaclass=MyMetaClass):  # MyMetaClass(User, (object,), {'x':10})
    
        '''我要成为年薪百万的男人,tank老师很好啊,向他学习'''
    
        def __init__(self):
            pass
    
        x = 10
        pass
    
    
    obj = User()
    
    print(obj)
    
    

    二、pymysql

    • 通过pymysql 去连接数据库
    # 下载第三方模块:在cmd 中下载
    
    # pip install pymysql
    
    
    
    # 面条版
    import pymysql
    
    # 1.连接数据库
    client = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='123456',
        database='db4',
        charset='utf8',  #此处不能写utf-8
        autocommit=True
    )
    
    # print(client)
    
    
    # 2.获取游标对象 ---->游标 可以用来提交sql命令
    course_obj = client.cursor(pymysql.cursors.DictCursor)
    
    
    # 3.通过execute 可以提交sql语句
    
    # 1) 查数据
    # sql = 'select * from emp'
    #
    # # 提交sql语句
    # course_obj.execute(sql)
    #
    # #4.提交后,通过cursor_obj 对象.fetchall()  获取所有查询到的结果
    #
    # res = course_obj.fetchall()
    # print(res)
    #
    # for dic in res:
    #     print(dic)
    
    # 2) 插入数据
    # 创建表
    # sql = 'create table user(id int, name varchar(16))'
    # course_obj.execute(sql)
    #
    # sql = 'insert into user(id, name) values(1, "yafeng")'
    # course_obj.execute(sql)
    
    
    # 注意得运行后才可以上传到数据库
    
    
    # 3) 更新数据
    # try:
    #     sql = 'update user set name="yafeng_很帅" where id=1'
    #
    #     course_obj.execute(sql)
    #
    # except Exception as e:
    #     print(e)
    
    # # 4) 删除数据
    # sql = 'delete from user'
    # course_obj.execute(sql)
    
    # 关闭游标
    # course_obj.close()
    #
    #
    # # 关闭客户端连接
    # client.close()
    
    
    
    Only you can control your future You're not alone. You still have family,peopel who care for you and want to save you.
  • 相关阅读:
    序列操作
    上帝造题的七分钟2 / 花神游历各国
    火柴排队
    pair(对组)用法
    线段树
    链上分治
    Rem与Px的转换
    css中单位px和em,rem的区别
    css网页自适应-1
    css网页自适应-2
  • 原文地址:https://www.cnblogs.com/yafeng666/p/12038225.html
Copyright © 2011-2022 走看看