与Python Falling In Love_Python跨台阶(面向对象)

第二课会介绍Python中的一些变量的使用、列表、元组、字典等一些详细内容。。。篇幅会比较多。。。因此在整理中。。。

先跳过第二课。。。直接来第三课。。Python中面向对象的学习以及与mysql数据库的连接!!!

Python也是一门面向对象语言,,,开始之前不得不夸下我真的。。。真的。。真的好喜欢Python啊啊啊!!!

 

下面我们就一起进入Python的对象世界把。。。

 

  Python中如何去定义、创建到使用一个类???

闲话不多说。。。看代码才是硬道理。。。

Python--类创建

'''
    @desc:基本类定义
    @author:Jimly
    @date:20160325 
'''
class Student:
   
    stuNum = 0  #公开变量
    __num = 0   #私有变量    使用两个下划线开头定义的变量
   
    def __init__(self, name, age):
        self.name = name
        self.age = age
        Student.stuNum += 1
        
        
    def showStudent(self):
        print(self.name, ' ', self.age)
        
'创建学生对象'
stu = Student("ZhangSan", 20)
stu1 = Student("LiSi", 21)            

'访问类中的属性'
stu.showStudent()
print('stuNum: %d' % Student.stuNum)
'程序报错 因为num属性为私有属性'
#print('stuNum: %d' % Student.num)

print('stu name:', getattr(stu, 'name'))    #返回name属性的值,如果不存在运行会报错
print('stu has sex?',  hasattr(stu, 'sex')) #判断stu对象是否存在sex属性,存在返回True,不存在返回False
setattr(stu, 'sex', 'man')  #设置sex属性值,如果不存在则创建一个属性
print('stu sex:', getattr(stu, 'sex'))
delattr(stu, 'sex') #删除一个属性
print('stu sex:', hasattr(stu, 'sex'))

程序输出:

ZhangSan   20
stuNum: 2
stu name: ZhangSan
stu has sex? False
stu sex: man
stu sex: False

 

  不像JAVA语言。。。Python是支持多继承滴。。。如class A(P1, P2, P3......)。。。

其中类P1,P2,P3就是父类。。。A就是继承了这么多父类的子类。。。

还有就是子类的构造方法中是不会默认调用父类的构造方法的。。。这一点也和JAVA有点出入。。。

Python--继承类

'''
    @desc:Python继承
    @author:Jimly
    @date:20160325
'''
class Grand:
    
    def __init__(self):
        print('Grand Constructor')

    def say(self):
        print('I am Grand')

class Parent:
    
    def __init__(self):
        print('Parent Constructor')
    
    def method(self):
        print('Parent method')


class Child(Parent, Grand):  # Child类继承Parent类和Grand类,,,Python支持多继承

    def __init__(self):
#         Grand.__init__(self)
#         Parent.__init__(self)
        print('Child Constructor')

    def method(self):  # 重写Parent类中的method方法
        print('Child method')

child = Child()
child.method()
child.say()

程序运行输出:

Child Constructor
Child method
I am Grand

 

Python--数据库连接

   想要通过Python代码连接mysql数据库,,,有MySQLdb和pymysql两种库可以用来连接mysql。。。

   但是Python3之后不再支持MySQLdb。。。由于我装的是Python 3.4.3.。因此只能用pymysql库咯。。。

    附:查看Python安装的版本  =》  在DOS窗口敲入命令:python --version

  

  首先在python官网下载ez_setup.py文件。。。如若链接失效、请访问https://pypi.python.org/pypi/setuptools/

    下载完ez_setup.py文件后,,,执行该文件。。。

   执行命令:  python ez_setup.py  如下图所示

    

   执行完之后会在Python的安装目录下的Scripts文件夹下生成一个easy_install.exe文件。。。如下图所示

        

   进入到该文件夹中。。执行命令:  easy_install.exe pymysql3  来安装pymysql3服务如下图所示

    

   pymysql3服务安装完之后,,,就可以动手写python连接mysql服务的程序咯。。。

  

实例代码如下:--查询案列

import pymysql

db = pymysql.connect(user='root', passwd='root',host='localhost', db='my_form')

sql = "select * \
        from form_frame"
        
cursor = db.cursor()

cursor.execute(sql)

for r in cursor:
    print('id:', r[0], ' rows:', r[1], ' cols:', r[2])

cursor.close()

db.close()

程序运行结果:

id: 5  rows: 111  cols: 111
id: 6  rows: 222  cols: 222
id: 7  rows: 333  cols: 333
id: 11  rows: 2015  cols: 111
id: 12  rows: 2015  cols: 222
id: 13  rows: 2015  cols: 333
id: 29  rows: 2015  cols: 111
id: 30  rows: 2015  cols: 222
id: 31  rows: 2015  cols: 333

 

注:import pymysql 可用别名替代、、、如:import pymysql as mysqldb 其中mysqldb可自定义的别名。。。

  此时db = pymysql.connect(user='root', passwd='root',host='localhost', db='my_form')需改为

    db = mysqldb.connect(user='root', passwd='root',host='localhost', db='my_form')

 

 新增案列代码:

import pymysql

db = pymysql.connect(user='root', passwd='root',host='localhost', db='my_form')

sql = "insert into form_frame(cols, rows) values(101, 201),(102, 202)"

try:
    cursor = db.cursor()

    res = cursor.execute(sql)

    db.commit()

    print(res)  #res为返回新增的记录条数
    
except:
    db.rollback() 

cursor.close()

db.close()

程序输出:2

  好了。。。今天就到这里了。。。

posted @ 2016-03-30 14:04  Answer.AI.L  阅读(601)  评论(0编辑  收藏  举报