Лог занятия по парадигмам, 28.09.2016
Материал из SEWiki
Версия от 14:16, 28 сентября 2016; E.f.suvorov (обсуждение | вклад) (Новая страница: «<pre> 1: >>> a=[] 2: >>> for i in range(10): ... a.append(i) ... 3: >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 4: >>> a=[] 5: >>> %rep 2 6: >>> a=…»)
1: >>> a=[] 2: >>> for i in range(10): ... a.append(i) ... 3: >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 4: >>> a=[] 5: >>> %rep 2 6: >>> a=[] 7: >>> for i in range(10): a.append(i * i) 8: >>> a [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] 9: >>> for i in range(10): a.append(i * i) 10: >>> a [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 0, 1, 4, 9, 16, 25, 36, 49, 64, 81] 11: >>> a=[0,0,0,2,2,4,4,5,6,6] 12: >>> ans=[(0,3), (2,2), (4,2), (5,1), (6,2)] 13: >>> out=[] 14: >>> %edit 'def lre_encode(a):\n if not a:\n out=[]\n else:\n out=[[a[0], 1]]\n for elem in a[1:]:\n if out[-1][0] == elem:\n out[-1][1] += 1\n else:\n out.append([elem, 1])\n return out\n' 15: >>> lre_encode(a) [[0, 3], [2, 2], [4, 2], [5, 1], [6, 2]] 16: >>> lre_encode([]) [] 17: >>> lre_encode([100]) [[100, 1]] 18: >>> lre_encode([100,100]) [[100, 2]] 19: >>> lre_encode([10] * 100) [[10, 100]] 20: >>> lre_encode([0]) [[0, 1]] 21: >>> a [0, 0, 0, 2, 2, 4, 4, 5, 6, 6] 22: >>> a[1:-1] [0, 0, 2, 2, 4, 4, 5, 6] 23: >>> as=[] 24: >>> xs=[] 25: >>> ys=[] 26: >>> zs=[] 27: >>> map(str, [1, 2, 3]) <map at 0x3413d70> 28: >>> list(map(str, [1, 2, 3])) ['1', '2', '3'] 29: >>> a=[1, 2, 3] 30: >>> b=[] 31: >>> for elem in a: ... b.append(str(elem)) ... 32: >>> b ['1', '2', '3'] 33: >>> map(str, a) <map at 0x342c4b0> 34: >>> list(map(str, a)) ['1', '2', '3'] 35: >>> a [1, 2, 3] 36: >>> a=[1,1,2,2,2,3,5,5] 37: >>> out=[] 38: >>> last_value=None 39: >>> last_length=0 40: >>> for elem in a: ... if elem == last_value: ... last_length += 1 ... else: ... out.append((last_value, last_length)) ... last_value = elem ... last_length = 1 ... 41: >>> out [(None, 0), (1, 2), (2, 3), (3, 1)] 42: >>> out.append((last_value, last_length)) 43: >>> out [(None, 0), (1, 2), (2, 3), (3, 1), (5, 2)] 44: >>> if name[0] == "." or name[0] == "~": continue 45: >>> name.startswith(".") 46: >>> a, a, b, a = range(4) 47: >>> a 3 48: >>> b 2 49: >>> %paste 50: >>> x=A() 51: >>> x=A(100) 52: >>> print(x) 53: >>> x.inc_foo() 54: >>> print(x) 55: >>> y=B() 56: >>> y=B(1000) 57: >>> print(y) 58: >>> y.inc_foo() 59: >>> print(y) 60: >>> class B(A): ... def __init__(self): ... print("B constructed") ... 61: >>> y=B() 62: >>> print(y) 63: >>> class B(A): ... def __init__(self): ... super(B, self).__init__(123) ... 64: >>> y=B() 65: >>> print(y) 66: >>> %paste 67: >>> x=A() 68: >>> x.foo() 69: >>> x.foo() 70: >>> y=A() 71: >>> y.foo() 72: >>> y.foo() 73: >>> del x.foo 74: >>> x.foo() 75: >>> del A.foo 76: >>> x.foo() 77: >>> del x.foo 78: >>> x.foo() 79: >>> A.foo = lambda self: print("self=", self) 80: >>> x.foo() 81: >>> A.__name__ 'A' 82: >>> type(x).__name__ 'A' 83: >>> x.__dict__ {} 84: >>> x.botva = 0 85: >>> x.__dict__ {'botva': 0} 86: >>> vars(x) {'botva': 0} 87: >>> vars(A) mappingproxy({'__weakref__': <attribute '__weakref__' of 'A' objects>, '__dict__': <attribute '__dict__' of 'A' objects>, '__doc__': None, '__module__': '__main__', 'foo': <function <lambda> at 0x034D54F8>}) 88: >>> A.__dict__ mappingproxy({'__weakref__': <attribute '__weakref__' of 'A' objects>, '__dict__': <attribute '__dict__' of 'A' objects>, '__doc__': None, '__module__': '__main__', 'foo': <function <lambda> at 0x034D54F8>}) 89: >>> isinstance(x, A) True 90: >>> class B: ... pass ... 91: >>> isinstance(x, B) False 92: >>> class C(A): ... pass ... 93: >>> x=C() 94: >>> isinstance(x, C) True 95: >>> isinstance(x, A) True 96: >>> isinstance(x, B) False 97: >>> x=A() 98: >>> isinstance(x, A) True 99: >>> isinstance(x, B) False 100: >>> isinstance(x, C) False 101: >>> issubclass(A, B) False 102: >>> issubclass(B, A) False 103: >>> issubclass(C, A) True 104: >>> issubclass(A, C) False