10.14 python笔记
2015-10-15 00:42
501 查看
python反射
from person import PersontheObj = globals()“Person”
print theObj.getName()
或
module = import(“person”)
theObj = getattr(module, “Person”)()
print theObj.getName()
/article/6267613.html
python类的定义
类的专有方法:init 构造函数,在生成对象时调用
del 析构函数,释放对象时使用
repr 打印,转换
setitem按照索引赋值
getitem按照索引获取值
len获得长度
cmp比较运算
call函数调用
add加运算
sub减运算
mul乘运算
div除运算
mod求余运算
pow称方
继承:
class 类名(父类1,父类2,….,父类n)
<语句1>
import
如果improt另外一个包的py文件出错,需要建立init.py文件/article/1279936.html
让MySQL查询结果返回字典类型
MySQLdb.connect( host=host, user=user, passwd=passwd, db=db, port=port, charset=’utf8′, cursorclass = MySQLdb.cursors.DictCursor ) 或 connection.cursor(cursorClass=MySQLdb.cursors.DictCursor)
/article/5038513.html
django不支持参数:cursorclass,如何返回字典?
def dictfetchall(cursor): "将游标返回的结果保存到一个字典对象中" desc = cursor.description return [ dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall() ]
http://www.yihaomen.com/article/python/250.htm
class特殊属性:
Class 有一些特殊的属性,便于我们获得一些额外的信息。 >>> class Class1(object): """Class1 Doc.""" def __init__(self): self.i = 1234 >>> Class1.__doc__ # 类型帮助信息 'Class1 Doc.' >>> Class1.__name__ # 类型名称 'Class1' >>> Class1.__module__ # 类型所在模块 '__main__' >>> Class1.__bases__ # 类型所继承的基类 (<type 'object'>,) >>> Class1.__dict__ # 类型字典,存储所有类型成员信息。 <dictproxy object at 0x00D3AD70> >>> Class1().__class__ # 类型 <class '__main__.Class1'> >>> Class1().__module__ # 实例类型所在模块 '__main__' >>> Class1().__dict__ # 对象字典,存储所有实例成员信息。 {'i': 1234}
django设置数据库
setting.py改成 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'dbname', # Or path to database file if using sqlite3. 'USER': 'username', # Not used with sqlite3. 'PASSWORD': '123456', # Not used with sqlite3. 'HOST': '127.0.0.1', # Set to empty string for localhost. Not used with sqlite3. 'PORT': 3306, # Set to empty string for default. Not used with sqlite3. } }
django设置静态文件路径
访问路径:127.0.0.1/static/123.js存放路径:projectRoot/staticFile/(manage.py同一层目录)
setting.py加上 STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'collected_static') STATICFILES_DIRS = ( os.path.join(BASE_DIR, "staticFile"), )
相关文章推荐
- Python3——简单的了解和简单操作
- Python正则表达式使用过程中的小细节
- Python爬虫正则表达式常用符号和方法
- 基于Python正则表达式提取搜索结果中的站点地址
- 使用Python解析JSON数据的基本方法
- 详细解读Python中解析XML数据的方法
- Python中将字典转换为XML以及相关的命名空间解析
- 简单介绍使用Python解析并修改XML文档的方法
- Ubuntu系统上Python2和Python3共存时安装BeautifulSoup4
- python学习—基础
- python ssh工具paramiko的修改
- python/django编码规范(code style)
- python----面向对象:1类的定义
- python脚本for循环
- 正则表达式匹配解析过程探讨分析(正则表达式匹配原理)
- Python编写简易木马程序
- Python(三) 2.7.6下的字符输入与输出
- Python 简介
- 【Python】Learn Python the hard way, ex39 字典操作
- python写的一个验证码小程序