Python3 3 day

  >>> class rectangle:
      def __init__(self,x,y)://(默认的___init__函数无默认参数,故根据需要重写__init__函数)
          self.a=x
          self.b=y      //__init__不能有返回值
      def getarer(self):  //self不是python得关键字,可以命名为别的
          return self.a * self.b
   
  >>> rect=rectangle(3,4)
  >>> rect.getarer()
  12

  >>> class C:
      def __init__(self):
          print("我是__init__方法")
      def __del__(self):
          print("我是__del__方法")
  
          
  >>> c1=C()//实例化对象时,先后自动调用__new__和__init__方法(两个方法加起来叫构造)
  我是__init__方法
 >>> c2=c1
 >>> c3=c2
 >>> del c2
 >>> del c1
 >>> del c3
 我是__del__方法//某个实例化对象的所有引用被del后,自动调用__del__方法(析构)
 >>> 

 

 【转】

{

在Python中 存在于类里面的构造方法__init__()负责将类的实例化,而在__init__()启动之前,__new__()决定是否 要使用该__init__()方法,因为__new__()可以调用其他类的构造方法或者直接返回别的对象来作为本类 的实例。

如果将类比喻为工厂,那么__init__()方法则是该工厂的生产工人,__init__()方法接受的初始化参 数则是生产所需原料,__init__()方法会按照方法中的语句负责将原料加工成实例以供工厂出货。而 __new__()则是生产部经理,__new__()方法可以决定是否将原料提供给该生产部工人,同时它还决定着出 货产品是否为该生产部的产品,因为这名经理可以借该工厂的名义向客户出售完全不是该工厂的产品

}

为什么重写__new__方法(经理在想,怎么处理这个原材料才能达到自己的目的):允许继承不可变类型(str int tuple)

 

1 >>> class C(str)://()内为一种数据类型对象
2     def __new__(cls,string): 
3         string=string.upper()//将参数转换为大写后
4         return str.__new__(cls,string)//调用基类__new__方法
5 
6     
7 >>> c1=C("hi china")
8 >>> c1
9 'HI CHINA'

 

 

 属性访问

 1 >>> class C:
 2     def __init__(self,size=10):
 3         self.size=size
 4     def getsize(self):
 5         return self.size
 6     def setsize(self,value):
 7         self.size=value
 8     def delsize(self):
 9         del self.size
10     x=property(getsize,setsize,delsize)
11 
12     
13 >>> c=C()
14 >>> c.x
15 10
16 >>> c.x=1
17 >>> c
18 <__main__.C object at 0x033A1F50>
19 >>> c.x
20 1
21 >>> c.size
22 1
23 >>> del c.x
24 >>> c.x
25 Traceback (most recent call last):
26   File "<pyshell#127>", line 1, in <module>
27     c.x
28   File "<pyshell#118>", line 5, in getsize
29     return self.size
30 AttributeError: 'C' object has no attribute 'size'
View Code

 迭代器(有两个魔法方法next iter)

它一次只返回一个数据项,占用更少的内存。但它需要记住当前的状态,以便返回下一数据项。

for语句自动调用迭代器

 1 >>> string="hyit"
 2 >>> it=iter(string)
 3 >>> next(it)
 4 'h'
 5 >>> next(it)
 6 'y'
 7 >>> next(it)
 8 'i'
 9 >>> next(it)
10 't'
11 
12 >>> string="hyit"
13 >>> it=iter(string)
14 >>> while True:
15     try:
16         each=next(it)
17     except StopIteration:
18         break
19     print(each)
20 
21     
22 h
23 y
24 i
25 t
View Code

 

 >>> for each in ('hyit'):# 等效于上面的那个
     print each

     
 h
 y
 i
 t

生成器

它基于yield指令,允许停止函数并立即返回结果

yield指令,可以暂停一个函数并返回中间结果。使用该指令的函数将保存执行环境,并且在必要时恢复。

只要函数中包含yield关键字,该函数调用就是生成器对象。

yield与return的异同:都可以返回值;return结束方法销毁中间值,yield暂停方法并保留中间值

  >>> def gen():
      yield 1
      yield 2
  
      
  >>> g=gen()
  >>> next(g)
  1
  >>> next(g)
  2
  >>> next(g)
 Traceback (most recent call last):
   File "<pyshell#31>", line 1, in <module>
     next(g)
 StopIteration

 模块

导入模块:import 模块名;import 函数名 from 模块名;import 模块名 as 新名字

__name__是文件的一个属性,在import 一个.py文件后,__name__的值就不是'__main__'了,python __name__ == __main__来判断是否是在直接运行该.py文件而不是被导入另一个文件运行

 >>> import math
 >>> math.__name__
 'math'
 >>> __name__
 '__main__'

 包

模块的上一级目录

导入方法:import 包名.模块名

搜索路径

为什么需要搜索路径?模块不一定放在同一个目录下,有了搜索路径这样方便使用

 >>> import sys
 >>> sys.path
 ['', 'F:\\安装软件\\python3\\Lib\\idlelib', 'C:\\Windows\\system32\\python33.zip', 'F:\\安装软件\\python3\\DLLs', 'F:\\安装软件\\python3\\lib', 'F:\\安装软件\\python3', 'F:\\安装软件\\python3\\lib\\site-packages']
 >>> sys.path.append("要导入的模块所在路径")

 爬虫

URL由三部分组成:protocol://hostname[:port]/path/[;parameters][?query]#fragment

 >>> import urllib.request
 >>> html=urllib.request.urlopen("http://www.baidu.com").read()
 >>>html=html.decode("utf-8")//把二进制码解码成网页的编码格式
 >>>print(html)

  >>> import urllib.request as ur
  >>> response=ur.urlopen("http://www.baidu.com")
  >>> response.info()
  <http.client.HTTPMessage object at 0x033939B0>
  >>> print(response.info())
  Date: Sat, 08 Aug 2015 12:27:11 GMT
  Content-Type: text/html; charset=utf-8Transfer-Encoding: chunked
  Connection: Close
 Vary: Accept-Encoding
 Set-Cookie: BAIDUID=BEEC065E8A97CA1BD7E379EA50A053E3:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
12 Set-Cookie: BIDUPSID=BEEC065E8A97CA1BD7E379EA50A053E3; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
13 Set-Cookie: PSTM=1439036831; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
14 Set-Cookie: BDSVRTM=0; path=/
15 Set-Cookie: BD_HOME=0; path=/
16 Set-Cookie: H_PS_PSSID=16059_16716_1426_16474_12657_12824_12868_16520_16799_16507_16663_16426_16515_15840_12062_13932_16721_10633; path=/; domain=.baidu.com
 P3P: CP=" OTI DSP COR IVA OUR IND COM "
 Cache-Control: private
 Cxy_all: baidu+d70d9733c917b5b9953ac2ef0603fa16
 Expires: Sat, 08 Aug 2015 12:26:49 GMT
 X-Powered-By: HPHP
 Server: BWS/1.1
 X-UA-Compatible: IE=Edge,chrome=1
 BDPAGETYPE: 1
 BDQID: 0xa21398f2000103e3
 BDUSERID: 0
>>> response.getcode()
200  //200表明正常响应

安装setuptools:https://pypi.python.org/pypi/setuptools/

 Python重要的内置容器:列表、元组、字典、集合、(map、reduce、filter)(迭代器与生成器、协同和半协同)

 

posted @ 2015-08-08 09:53  沐风先生  阅读(149)  评论(0编辑  收藏  举报