• python题目训练(随时更新)


    1.

    1 def func(arg,li=[]):
    2     li.append(arg)
    3     return li
    4 v1 = func(1)
    5 v2 = func(2,[])
    6 v3 = func(3)
    7 print(v1)
    8 print(v2)
    9 print(v3)

    最终输出结果为:
    [1,3]
    [2,]
    [1,3]

    1 def func(arg,li=[]):
    2     li.append(arg)
    3     return li
    4 v1 = func(1)
    5 print(v1)
    6 v2 = func(2,[])
    7 print(v2)
    8 v3 = func(3)
    9 print(v3)

    最终输出结果为:
    [1]
    [2,]
    [1,3]

    执行v1时,li就创建在内存,v2时,传入新的[],所欲i创建另一内存,v3时使用v1创建的内存

    2.

     1 n1 = [11,22,33,44,55]
     2 n2 = n1
     3 n3 = n1[:]
     4         
     5 n1[0]=666
     6 n3[1]=999
     7         
     8 print(n1)
     9 print(n2)
    10 print(n3)

    最终结果为:
    [666, 22, 33, 44, 55]
    [666, 22, 33, 44, 55]
    [11, 999, 33, 44, 55]

    因为n2 = n1,所以二者指向同一内存,n3 = n1[:]为切片,重新复制一份给n3

    2018-09-06 13:34:41

    3.

     1 var v = 123;
     2 function foo(){
     3     var v = 456;
     4     function inner(){
     5         console.log(v)
     6     }
     7     return inner
     8 }
     9 
    10 result = foo()
    11 console.log(result())

    输出结果:

    456

    4.

     1 Name='root';
     2 Age = 18;
     3 function Foo(name,age){
     4     this.Name = name;
     5     this.Age = age;
     6     this.Func = function(){
     7         console.log(this.Name,this.Age);
     8         (function(){
     9              console.log(this.Name,this.Age);
    10         })();
    11     }
    12 }
    13 obj = new Foo('alex',666);
    14 obj.Func()

    输出结果:
    alex',666
    root 18

    3.4解析:

     1 this关键字:
     2     每个函数中都有this
     3     函数调用时,this=window
     4     类new时, this=obj
     5     
     6     function func(){
     7         #当作函数执行函数时,this=window
     8         console.log(this)
     9     }
    10     func()
    11     
    12     function Func(){
    13         
    14     }
    15     obj = new Func()
    16 
    17 js中无字典,只有对象:
    18     Name = 'lxs'
    19     obj = {
    20         name:'root',
    21         age:18,
    22         Func:function(){
    23         #this = obj
    24             console.log(this.name)   #root
    25             function.inner(){
    26                 #this = window
    27                 console.log(this.Name)    #lxs
    28             }
    29             inner() #相当于window.inner()
    30         
    31         }
    32     
    33     }
    34     相当于new成了对象obj
    35     通过一个对象调用类内部的方法时,this = 对象
    36     obj.Func()  Func内部的对象 = obj
    37     结果为:
    38     root
    39     lxs
    40     
    41     Name = 'lxs'
    42     obj = {
    43         name:'root',
    44         age:18,
    45         Func:function(){
    46         #this = obj
    47             console.log(this.name)   #root
    48             var that = this
    49             function.inner(){
    50                 
    51                 console.log(that.Name)    #root
    52             }
    53             inner() #相当于window.inner()
    54         
    55         }
    56     
    57     }
    58     obj.Func()
    59     结果为:root root
    60     
    61     
    62     自执行函数:
    63         function.inner(){
    64                 
    65                 console.log(that.Name)    
    66             }()
    67         相当于:
    68             function.inner(){
    69                 
    70                 console.log(that.Name)    
    71             }
    72             inner() 

     

     

  • 相关阅读:
    mysql的存储过程
    一份php高级工程师的面试题,我每天看一点点
    php的常用函数(持续更新)
    php 中文字符串截取
    php递归遍历文件目录
    ajax timeout 断网处理
    angular 刷新问题
    angular state中templateUrl 路径的模板
    angular请求传递不了数据
    截取字符串 substring substr slice
  • 原文地址:https://www.cnblogs.com/GodLv/p/9592848.html
Copyright © 2020-2023  润新知