10.14 python笔记

来源:互联网 发布:程序员刚入职很痛苦 编辑:程序博客网 时间:2024/05/16 18:46

python反射

from person import Person
theObj = globals()“Person”
print theObj.getName()

module = import(“person”)
theObj = getattr(module, “Person”)()
print theObj.getName()
http://www.cnblogs.com/zjgtan/p/3539808.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文件
http://www.jb51.net/article/54323.htm

让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)

http://www.cnblogs.com/coser/archive/2012/01/12/2320741.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"),)
0 0