Python 作用域
定义
变量起作用的范围
ps:对写代码并非必要,但是了解作用域有助于写出更优秀的代码
- j局部作用域
-在Python的嵌套代码中的变量,只在代码块中起作用要注意的时,流程控制不会形成局部作用域。
>>> print (__name__) #L(内建作用域)
__main__
>>> name = "cslcf"#(全局作用域)
>>> def fun() :
... name = "asxlt"#(局部作用域)
... def inner():
... print (name)#(嵌套作用域)
...
>>> fun()
>>> print(locals)#查看局部作用域
<built-in function locals>
>>> print (globals())#查看全局作用域
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'breakpointhook': <built-in function breakpointhook>, 'call
Python 的递归
定义:
程序调用自身的编程技巧称之为递归
递归函数的两个要求:
-
在自身内部调用自身
-在函数满足某个条件调用当前函数 -
有穷性
-一定有一个不满足的调整
举例
递归算阶乘
>>> def jc(number):
... if number<=1 :
... return 1
... else :
... return number*jc(number-1)
...
>>> print(jc(5))
120
装饰器
定义:
在不改变原函数的基础上,给函数增加功能
把一个函数当作参数,返回一个替代版的函数
本质上:返回函数的函数
装饰器的推导
>>> def outer() :
... print('this is outer')
...
>>> print(outer) #函数本身
this is outer
None
>>> outer()# 函数的调用
this is outer
>>> def outer():
... def inner() :
... print('this is outer')
...
此时直接执行outer()不会出inner的结果,想要内层函数执行在外层函数再加一对括号outer()();或者在函数内部直接调用内层函数
再看这个
>>> outer()
>>> print(outer)
<function outer at 0x02FE95D0>
>>> print(outer())
用到函数的闭包
再看这个 稍改了一下
>>> def outer():
... def inner() :
... print('this is outer')
... return inner #返回函数的本身
...
>>> outer() #返回inner函数
<function outer.<locals>.inner at 0x02FE95D0>
>>> outer()() #inner函数的调用
this is outer
这就完成了函数的闭包
变量指向函数
>>> def outer (fun):
... def inner():
... print("this is my name is cslcf")
... fun()
... return inner
>>> def hello():
... print ("hello wold")
...
# 测试:
>>> print(hello)
<function hello at 0x02FE9540>
>>> hello = outer(hello)
>>> print(hello)
<function outer.<locals>.inner at 0x02FE9738>
>>> hello()
this is my name is cslcf
hello wold
最简单的装饰器
>>> def outer (fun):
... def inner():
... print("this is my name is cslcf")
... fun()
... return inner
@outer
>>> def hello():
... print ("hello wold")
...
装饰器是用来给所有函数添加共性功能的,所以编写装饰器首先要考虑所有函数共性的需求
举例:
自我介绍中同专业的在介绍中专业相同,是共性,而方向不同是特点
>>> def outer(fun):
... def inner():
... print("大家好我的专业是计算机")
... fun()
... print("自我介绍完了,谢谢大家")
... return inner
>>> @outer
... def tiyu():
... print("我的方向是信息安全")
...
>>> @outer
... def yinyue():
... print("我的方向是web开发")
...
>>> tiyu()
大家好我的专业是计算机
我的方向是信息安全
自我介绍完了,谢谢大家
Python 代码好美 T_T、
Python 文件操作
- Python 文件
Python 对文件的操作是通过内置的open()函数打开一个文件,常见一个file对象,相关的方法才可以调用它进行读写
file = open (file_name [,access_mode][,buffering0)
可以传三个参数
file_name 文件路径,不单单是名称
access_mode 文件的访问权限
buffering 是否缓存
- Python文件权限
r 读
w 覆盖写
a 追加写
rb 二进制读
wb 二进制写
- Python文件的基本操作
>>> f = open('1.txt','r')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '1.txt'
>>> f = open('1.txt','w') #目录下没有此文件,会创建文件
>>> f.write("hello wold")
10
>>> path = os.getcwd()
>>> print (path)
C:\Users\Administrator
>>> f.close() 执行后保存对文件的修改
>>>
对大量文件操作时,小号时间最多的是打开和关闭。
对文件内容大的文件读操作时,为了避免一次性读写卡死,进行行输出:
f = open("1.txt")
for line in f.readlines():
print(line)
f.close()
二进制文件举例
复制的功能:
f = open ("1.jpg","rb")
l = open ("2.jpg","wb")
l.write(f.read())
l.close()
f.close()
完成了将文件从1.jpg写入2.jpg。
f.tell: 返回当前指针在文章的
>>> f = open ("1.txt")
>>> print(f.tell())
0
>>> print(f.readlines())
['hello wold']
>>> print(f.tell())
10
>>> f.close()
Python 面向对象
就不说说了,php也是
>>> class Dog():
... tz = 40
... def sing(self,name):
... self.name=name
... print("汪汪汪 I am %s"%self.name)
... def hello(self):
... print("hello I am %s" %self.name)
...
>>> d = Dog()
>>> d.sing("td")
汪汪汪 I am td
>>> d.hello()
hello I am td
- 私有变量 ,变量前加两个下划线
>>> class Dog():
... __age = 3 #变量前加两个下划线形成私有变量,只能内部调用,所以程序报错
...
>>> d = Dog()
>>> d.__age
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Dog' object has no attribute '__age'
- 继承
将共性的方法抽象到父类中,子类在定义的时候,继承父类,只定义自己独有的方法
>>> class Bird:
... def sing(self):
... print("sing sing")
...
>>> class Mq(Bird): #继承鸟类
... def sing(self) #子类可以覆盖父类的方法
... print("eat 小米")
... Bird.sing(self) #这里时继承了父类也添加了机子的东西,此时的self时Mq的而不是Bird的
...
>>> m = Mq()
>>> m.sing()
eat 小米
sing sing
- 多态
在类当中,同一个方法,遇到不同的实例,会有不同的处理方式
简单的例子 :
>>> class Bird():
... def eat(self):
... if self.name == "mq":
... print ("eat 小米")
... else:
... print("eat 肉")
...
>>> b = Bird()
>>> b.name = "mq"
>>> b.eat()
eat 小米
>>> b.name = "老鹰"
>>> b.eat()
eat 肉
- 模块的使用
class People(BaseObj):
def eat(self):
print('人吃')
def say(self):
print('人说')
def sleep(self):
print('人睡')
class Dog(BaseObj):
def eat(self):
print('狗吃')
def say(self):
print('狗说')
def sleep(self):
print('狗睡')
class Action:
def __init__(self,obj):
self.obj = obj
def eat(slef):
self.obj.eat()
def say(slef):
self.obj.say()
def sleep(slef):
self.obj.sleep()
People = Action(Dog())
people.say()
people.eat()
people.sleep()
#报错了 没有继续深究
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'BaseObj' is not defined
Python 魔术方法
在Python 中,所有以“__ " 双下划线抱起来的方法都统称为 “Magic Method”,中文称称【魔术方法】,例如类的初始化方法__init__,Python 中所有的魔术方法在官方文档中有相应的描述
__ init __(self[…]) 构造器,当一个实例被创建的时候调用的初始化方法
__del __(self) 构造器,当一个实例被销毁的时候调用的方法
__ call __(self[,args…]) 云溪一个类的实例像函数一样被调用:x(a,b) 调用 x. __ call __(a,str(self))定义当被 len() 调用时的行为
ps:__ init __ 函数中的return不能带返回值