所有的这些是来自于Python中文社区的基础教程内容,也许我们并不需要掌握所有的语法知识就可以完成大部分的工作,但是作为兴趣或者你工作累了的时候,可以调节紧张的情绪。
- Python的数据类型:
Python的数据类型分为两种,一种是标准的数据类型,还有是复杂的,核心编程上称之为复杂的数据类型
标准的数据类型:
Integer 整型
Boolean 布尔型
Long integer 长整型
Float 浮点型
Complex 复数型
String 字符串
List 列表
Tuple 元组
Dictionary 字典
其他的数据类型包括:
Type型
类
函数
模块
文件
Null对象(None)
- range的使用
for j in range(20): print("hello"+str(j))
输出结果为:
hello0
hello1hello2hello3.......
Python提供的循环技术有for循环和while循环方式:
- Python中的class
Python是面向对象的语言,和C++类似,关键字class后面紧跟一个类名:然后就是给该类定义的一些方法和属性。
class IEtest: """this is a class example""" _version = 1.0, _author = 'ywc', def __init__(self): print('init the class'); def setVersion(self,version): self._version = version; #return self._version; def setAuthor(self,author): self._author = author; #return self._author; def getVersion(self): print(self._version); def getAuthor(self): print(self._author);if __name__ == "__main__": ie = IEtest();
如图所示:需要注意的几个地方:
(1)我们一般将_##这样的变量定义为类的私有变量,这是命名习惯问题;
(2)def__init__(self):类似于类的构造函数,每定义一个对象的时候都会自动调用该函数。需要注意的是我们必须在__init__()中添加参数。否则会报错!
英文原文为:
-
The first method __init__() is a special method, which is called class constructor or initialization method that Python calls when you create a new instance of this class.
(3)self类似于对象的this指针。
(4)The class has a documentation string, which can be accessed via ClassName.__doc__.
这句话的意思是__doc__是一个内建函数,通过这个函数,我们可以得到代码中关于三引号内的文档说明;
类有四个比较重要的函数可以操作属性:
-
The getattr(obj, name[, default]) : to access the attribute of object.
-
The hasattr(obj,name) : to check if an attribute exists or not.
-
The setattr(obj,name,value) : to set an attribute. If attribute does not exist, then it would be created.
-
The delattr(obj, name) : to delete an attribute.
分别可以得到/判断/设置/删除对象的某个属性
if __name__ == "__main__": ie = IEtest(); print(hasattr(ie,'_version'))#true print(hasattr(ie,'setVersion'))#true print(hasattr(ie,'setVersion()'))#false
注意,我们判断属性的时候不需要写成第三种情况,因此我们才得到一个false的结果。我们还可以通过查看函数dir来实现查看对象所具有的属性和方法:
>>> dir(ie)['__doc__', '__init__', '__module__', '_author', '_version', 'getAuthor', 'getVersion', 'setAuthor', 'setVersion']
Every Python class keeps following built-in attributes and they can be accessed using dot operator like any other attribute:
-
__dict__ : Dictionary containing the class's namespace.
-
__doc__ : Class documentation string or None if undefined.
-
__name__: Class name.
-
__module__: Module name in which the class is defined. This attribute is "__main__" in interactive mode.
-
__bases__ : A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list
每一个Python类都有下面四个内建函数。__doc__我们已经很熟悉。__module__我们后面将会介绍。
对象的销毁:
a = 40 # Create object <40>b = a # Increase ref. count of <40> c = [b] # Increase ref. count of <40> del a # Decrease ref. count of <40>b = 100 # Decrease ref. count of <40> c[0] = -1 # Decrease ref. count of <40>
首先将40这个对象赋值给变量a以后,40这个对象的计数器就加1. 然后把a的值赋给b,此时,a和b指向相同的对象
- Python的继承
class IEtest: """this is a class example""" _version = 1.0, _author = 'ywc', def __init__(self): print('init the class'); def setVersion(self,version): self._version = version; #return self._version; def setAuthor(self,author): self._author = author; #return self._author; def getVersion(self): print(self._version); def getAuthor(self): print(self._author);class Inherent(IEtest): """this is inherend from the parent""" def __init__(self): print("the parent method is called"); if __name__ == "__main__": ie = IEtest(); print(hasattr(ie,'_version'))#true print(hasattr(ie,'setVersion()')) print('-----------------------'); inherent = Inherent(); print(inherent._version);
输出结果:
>>> init the classTrueFalse-----------------------the parent method is called(1.0,)
继承的语法为:
关键字class 类名 (继承的对象)
验证一下继承后子类的参数情况:
['__doc__', '__init__', '__module__', '_author', '_version', 'getAuthor', 'getVersion', 'setAuthor', 'setVersion']['__doc__', '__init__', '__module__', '_author', '_version', 'getAuthor', 'getVersion', 'setAuthor', 'setVersion']
第一行是父类的方法和属性,第二行是派生类的方法和属性
- Python的重载
You can always override your parent class methods. One reason for overriding parent's methods is because you may want special or different functionality in your subclass.
class IEtest: """this is a class example""" _version = 1.0, _author = 'ywc', def __init__(self): print('init the class'); def setVersion(self,version): self._version = version; #return self._version; def setAuthor(self,author): self._author = author; #return self._author; def getVersion(self): print(self._version); def getAuthor(self): print(self._author); def commonMethod(self): print("the parent method is called in this Python");class Inherent(IEtest): """this is inherend from the parent""" def __init__(self): print("the parent method is called"); def commonMethod(self): print("the child method is called in this python") if __name__ == "__main__": ie = IEtest(); print(hasattr(ie,'_version'))#true print(hasattr(ie,'setVersion()')) print('-----------------------'); inherent = Inherent(); print(inherent._version); print(dir(inherent)) print(dir(ie)) print('-----------------------'); ie.commonMethod(); inherent.commonMethod();
输出结果:
the parent method is called in this Pythonthe child method is called in this python
问题是当派生类中定义了父类的方法commonMethod后,怎样调用父类的该方法?
print("How we call the parent methid?\n\ from the last etc we can see that we can use the\n\ the parent obj to call. Now we conside how to call\n\ the parent obj using the child obj\n") print("----Method1----") IEtest.commonMethod(inherent); print("----Method2----") super(Inherent).commonMethod();#important
在上面我们提出了两种解决办法。关键是第二个。就是应用从Java中演变来的super关键字。需要注意的是:
super() can be used only in the new-style classes, which means the root class needs to inherit from the 'object' class.For example, the top class need to be like this:class SomeClass(object): def __init__(self): ....notclass SomeClass(): def __init__(self): ....So, the solution is that call the parent's init method directly, like this way:class TextParser(HTMLParser): def __init__(self): HTMLParser.__init__(self) self.all_data = []