20190820 Python 练习记录

Python 3.7.2 (bundled)
>>> class student(object):
    pass

>>> s=student()
>>> s.name ='sxj'
>>> print(s.name)
sxj
>>> def set_age(self,age):
    self.age =age
    
>>> from types import MethodType
>>> s.set_age=MethodTpye(set_age,s)
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
NameError: name 'MethodTpye' is not defined
>>> s.set_age=MethodType(set_age,s)
>>> s.set_age(25)
>>> s.age
25
>>> def set_score(self,score):
    self.score=score
    
>>> student.set_score=89
>>> s.score
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
AttributeError: 'student' object has no attribute 'score'
>>> student.score
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
AttributeError: type object 'student' has no attribute 'score'
>>> student.set_score=set_score
>>> s.set_score(410)
>>> s.score
410
>>> class student(object):
    __slots__=('name','age')
    
>>> s=student()
>>> s.name='xj'
>>> s.age=78
>>> s.score=45
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
AttributeError: 'student' object has no attribute 'score'
>>> class student(object):
    def get_score(self):
        return self.score
    def set_score(self,value):
        if not isinstance(value,int):
            raise valueerror('错误的输入数据!')
        if value <0 or value >100:
            raise valueerror('score must between 0~100')
        self.score=value
        
>>> s=student()
>>> s.set_score(20)
>>> s.get_score()
20
>>> s.set_score(101)
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
  File "<pyshell>", line 8, in set_score
NameError: name 'valueerror' is not defined
>>> s.set_score('4')
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
  File "<pyshell>", line 6, in set_score
NameError: name 'valueerror' is not defined
>>> class student(object):
    @property
    def score(self):
        return self.score
    @score.setter
    def score(self,value):
        if not isinstance(value,int):
            raise ValueError('错位u的书籍输入')
        if value <0 or value >100:
            raise ValueError('分数必须在0到100')
        self.score=value
        
>>> s=student()
>>> s.score=78
File "<pyshell>", line 11, in score
>>> class student(object):
    @property
    def score(self):
        return self._score
    @score.setter
    def score(self,value):
        if not isinstance(value,int):
            raise ValueError('错位u的书籍输入')
        if value <0 or value >100:
            raise ValueError('分数必须在0到100')
        self._score=value
        
>>> s=student()
>>> s._score=89
>>> s._score
89
>>> s._score=101
>>> s._score=501
>>> s._score
501
>>> s.score=89
>>> s.score
89
>>> s.score=101
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
  File "<pyshell>", line 10, in score
ValueError: 分数必须在0到100
>>> s.score='1'
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
  File "<pyshell>", line 8, in score
ValueError: 错位u的书籍输入
>>> 
>>> class student(object):
    def __init__(self,name):
        self.name=name
        
>>> print(student('jx'))
<__main__.student object at 0x034919F0>
>>> class student(object):
    def __init__(self,name):
        self.name=name
    def __str__(self):
        return 'student object(name:%s)' %self.name
    
>>> print(student('xhk'))
student object(name:xhk)
>>> class student(object):
    def __init__(self,name):
        self.name=name
    def __str__(self):
        return 'student object(name:%s)' %self.name
    __repr__=__str__
    
>>> s=student('xkl')
>>> s.name
'xkl'
>>> class fib(object):
        def __init__(self):
            self.a,self.b=0,1
        def __iter__(self):
            return self
        def __next__(self):
            self.a,self.b=self.b,self.a+self.b
            if self.a>100000
                raise StopIteration()
            return self.a
        
  File "<pyshell>", line 8
    if self.a>100000
                   ^
SyntaxError: invalid syntax
>>> class fib(object):
        def __init__(self):
            self.a,self.b=0,1
        def __iter__(self):
            return self
        def __next__(self):
            self.a,self.b=self.b,self.a+self.b
            if self.a>100000
                raise StopIteration()
            return self.a
        
  File "<pyshell>", line 8
    if self.a>100000
                   ^
SyntaxError: invalid syntax
>>> 
>>> class fib(object):
        def __init__(self):
            self.a,self.b=0,1
        def __iter__(self):
            return self
        def __next__(self):
            self.a,self.b=self.b,self.a+self.b
            if self.a>100000:
                raise StopIteration()
            return self.a
        
>>> for n in fib():
        print(n)
        
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
>>> class fib(object):
        def __getitem__(self,n):
            a,b=1,1
            for x in range(n):
                a,b=b,a+b
            return a
        
>>> f=fib()
>>> f[0]
1
>>> f[100]
573147844013817084101
>>> f[10:20]
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
  File "<pyshell>", line 4, in __getitem__
TypeError: 'slice' object cannot be interpreted as an integer
>>> class fib(object):
    def __getitem__(self,n):
        if isinstance(n,int):#n是索引
            a,b=1,1
            for x in range(n):
                a,b=b,a+b
            return a
        if isinstance(n,slice):#n是切片
            start=n.start
            stop=n.stop
            if start is None
                start =0
            a,b=1,1
            L=[]
            for x in range(stop):
                if x>=start:
                    L.append(a)
                a,b=b,a+b
            return L
        
  File "<pyshell>", line 11
    if start is None
                   ^
SyntaxError: invalid syntax
>>> class fib(object):
    def __getitem__(self,n):
        if isinstance(n,int):#n是索引
            a,b=1,1
            for x in range(n):
                a,b=b,a+b
            return a
        if isinstance(n,slice):#n是切片
            start=n.start
            stop=n.stop
            if start is None:
                start =0
            a,b=1,1
            L=[]
            for x in range(stop):
                if x>=start:
                    L.append(a)
                a,b=b,a+b
            return L
        
>>> range()
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
TypeError: range expected 1 arguments, got 0
>>> range(5)
range(0, 5)
>>> f=fib()
>>> f[2:10]
[2, 3, 5, 8, 13, 21, 34, 55]
>>> f[:100:2]
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135, 308061521170129, 498454011879264, 806515533049393, 1304969544928657, 2111485077978050, 3416454622906707, 5527939700884757, 8944394323791464, 14472334024676221, 23416728348467685, 37889062373143906, 61305790721611591, 99194853094755497, 160500643816367088, 259695496911122585, 420196140727489673, 679891637638612258, 1100087778366101931, 1779979416004714189, 2880067194370816120, 4660046610375530309, 7540113804746346429, 12200160415121876738, 19740274219868223167, 31940434634990099905, 51680708854858323072, 83621143489848422977, 135301852344706746049, 218922995834555169026, 354224848179261915075]
>>> 
>>> class student(object):
    def __init__(self):
        self.name='ksuy'
    def __getattr__(self,attr):
        if attr=='score'
            return 90
        
  File "<pyshell>", line 5
    if attr=='score'
                   ^
SyntaxError: invalid syntax
>>> class student(object):
    def __init__(self):
        self.name='ksuy'
    def __getattr__(self,attr):
        if attr=='score':
            return 90
        
>>> s=student()
>>> s.name
'ksuy'
>>> s.score
90
>>>  class student(object):
    def __init__(self):
        self.name='ksuy'
    def __getattr__(self,attr):
        if attr=='score':
            return lambda:24
        
>>> s.score
90
>>> s=student()
>>> s.score
<function student.__getattr__.<locals>.<lambda> at 0x03498228>
>>> 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值