您的位置:首页 > 编程语言 > Python开发

10.14 python笔记

2015-10-15 00:42 501 查看

python反射

from person import Person

theObj = 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"),
)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: