元类, pymysql
1、元类
'''
一、什么是元类?
- 类的类就是type,其实type就是元类
二、元类的做用?
三、如何建立元类以及使用?
'''
# # 一、一切皆对象
# list1 = [] # list1 = list([])
# print(type(list1)) #<class 'list'>
#
# # # 二、自定义一个类
# 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'>,
# 三、如何产生类
#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)
#一、什么是元类?
# 类的类就是type,其实type就是元类
# 二、为何要使用元类?
# 由于元类能够控制类的建立过程
#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类括号中的值
# 一、造一个空对象obj
obj = object.__new__(self) # 创造一个空对象 self ---> User
print(obj.__dict__, 1111111)
# 二、调用类时,__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)
2、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()