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() 

 

 

 

转载于:https://www.cnblogs.com/GodLv/p/9592848.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值