python_11.pdf

(77 KB) Pobierz
75065206 UNPDF
Kurs języka Python
Wykład 11.
Atrybuty
Iteratory
Introspekcje (refleksje)
Dynamiczne generowanie kodu
Funkcje i operatory wbudowane
cmp(x,y)
d[i]
len(collection)
d[i] = v
str(obj)
del v[i]
<
>
==
+, -, * , /
....
Zadanie
Implementacja klasy wektorów wraz z
operatorami +, -, ...
Implementacja
class Vector:
def __init__( self , lista):
self .value = lista
dodawanie
def __add__( self , arg):
if self .__class__ <> arg.__class__ or \
len( self .value) <> len( arg .value):
return None
res = \
Vector( [x + y for x,y in zip( self .value,arg.value) ])
return res
Zgłoś jeśli naruszono regulamin