zoukankan      html  css  js  c++  java
  • python-day11

    一. 第一类对象, 函数名的使用

      函数名就是变量名,函数名存储的是函数的内存地址

      变量的命名规范:

        1. 有数字,字母,下划线组成

        2. 不能是数字开头,更不能是纯数字

        3. 不要用关键字

        4. 不要太长

        5. 要有意义

        6. 不要用中文

        7. 区分大小写

        8. 驼峰或者下划线

    def __():
        print(12345)
    __()
     1 # def fun1():
     2 #     print(1)
     3 # def fun2():
     4 #     print(2)
     5 # def fun3():
     6 #     print(3)
     7 #
     8 # l = [fun1,fun2,fun3,]
     9 # print(l)
    10 # for i in l:
    11 #     i()
    12 
    13 # def outer():
    14 #     def inner():
    15 #         print('我是可怜的inner')
    16 #     return inner#函数名可以作为返回值
    17 # ret = outer()
    18 # ret()
    19 
    20 #代理模式
    21 #装饰器的雏形
    22 #把函数名当成变量来使用
    23 # def chi(fn):#fn 代理了func1 和func2
    24 #     print('开挂')
    25 #     fn()
    26 #     print(fn.__name__)
    27 #     print('洗包')
    28 # def play_dnf():
    29 #     print('疯狂的书刷')
    30 # def func1():
    31 #     print('我是func1')
    32 # def func2():
    33 #     print('我是func2')
    34 # def he():
    35 #     print('我要喝酒')
    36 # chi(func1)
    37 # chi(func2)
    38 
    39 # def outer():
    40 #     def inner():
    41 #         print('哈哈')
    42 #     return inner
    43 # outer()() #outer()返回inner
    View Code

    二. 闭包

      闭包:在内层函数中引入外层函数(非全局)的变量

      可以使用__closure__来检测函数是否是闭包,使用.__closure__返回cell就是闭包,返回None就不是闭包

    1 def func1():
    2     name = 'alex'
    3     def fun2():
    4         print(name)#闭包
    5     fun2()
    6     print(fun2.__closure__)#(<cell at 0x04D71470: str object at 0x04C69600>,)
    7 func1()
    View Code

      作用:

        1. 保护变量不受侵害(JavaScript)

        2. 让一个变量常驻内存

     1 def func():
     2     def fun1():
     3         def fun2():
     4             print('哈哈')
     5         return fun2
     6     return fun1
     7 func()()()
     8 
     9 # print(func.__doc__) # document 文档注释
    10 # print(func.__name__) # name 名字 获取函数名
    View Code

    三. 迭代器

      dir()  查看变量能够执行的方法(函数)

      Iterator:   迭代器,  __iter__(),  __next__()

      Iterable:   可迭代的,__iter__()

      

      for循环的流程:

      it = lst.__iter__()

      while 1:

        try:

          el = it.__next__()

          for 循环的循环体

        except  StopIteration:

          break

        从迭代器中获取数据的唯一方法: __next__()

     1  print(dir(str))#查看str能够执行的操作,内部的方法 (list,dict,)都有
     2 # 简单的下一个结论,主要这个数据类型可以执行__iter__ 可以被迭代的数据类型
     3 lis = [1,2,3,4,5,]
     4 # it = lis.__iter__()
     5 # print(it)#<list_iterator object at 0x04FA1450>   iterator 迭代器
     6 # print(dir(it)) #迭代/器本身是可迭代的
     7 #可以使用__next__获取数据
     8 # print(it.__next__())
     9 # print(it.__next__())
    10 # print(it.__next__())
    11 # print(it.__next__())
    12 # print(it.__next__())
    13 # print(it.__next__()) #StopIteration迭代器中没有元素了
    14 
    15 # for 循环内部的代码
    16 # it = lis.__iter__()
    17 # while 1 :
    18 #     try:
    19 #         el = it.__next__()
    20 #         print(el)
    21 #     except StopIteration:
    22 #         # print('结束了')
    23 #         break
    24 
    25 # 三个特点
    26 #     1. 节省内存(生成器)
    27 #     2. 惰性机制,必须用__next__()来获取数据
    28 #     3. 只能往前,不能后退
    29 
    30 # list(内部有for循环)
    31 # for内部 迭代器
    32 
    33 # ls = [1,1,1,1,1,1,1,1,1,1,122,22]
    34 # ll = list(set(ls))#去重
    35 # print(ll)
    36 
    37 # list(1)   #'int' object is not iterable #不可迭代的
    38 
    39 # 如何判断一个数据是否是可迭代对象
    40 # 1. dir() ->__iter__ 可迭代的
    41 # 2. dir() -> __next__ 迭代器
    42 # print('__iter__' in dir(int)) #False
    43 # print('__next__' in dir(int))#False
    44 
    45 # collections 关于集合类的相关操作
    46 # Iterable: 可迭代的
    47 # Iterator: 迭代器
    48 # from collections import Iterable,Iterator
    49 # print(isinstance(int,Iterable)) #True
    50 # print(isinstance(int,Iterator)) #Falsse
    51 # 
    52 # print(isinstance({1,2,3},Iterable)) #True
    53 # print(isinstance({1,2,3}.__iter__(),Iterator)) #True
    View Code

        三个特征:

          1. 省内存

          2. 惰性机制

          3. 只能往前,不能后退

  • 相关阅读:
    程序员练手项目
    文件描述符
    安卓深度探索(卷一)第九章
    安卓深度探索(卷一)第十章
    安卓深度探索(卷一)第八章
    安卓深度探索(卷一)第六章
    安卓深度探索(卷一)第七章
    安卓深度探索(卷一)第五章
    记一次联想A820t救砖线刷
    记一次酷派尚锋Y75刷机
  • 原文地址:https://www.cnblogs.com/Thui/p/9883549.html
Copyright © 2011-2022 走看看