class PhoneNumber : def __init__(self,c,z,pr,po) : self.country = c self.zone = z self.prefix = pr self.postfix = po def call(self) : print("Calling number " + str(self)) def __str__(self): return "+" + self.country + "(0)" + self.zone \ + "/" + self.prefix + self.postfix
>>> number_kim = PhoneNumber("32","10","4","79111") >>> print(number_kim) +32(0)10/479111 >>> number_kim = PhoneNumber("32","10","4","79111") >>> print(number_kim) +32(0)10/479111 >>> number_kim.call() Calling number +32(0)10/479111
class NokiaPhone : def __init__(self,s,p,t,n) : self.marque = "Nokia" self.serie = s self.poids = p self.taille = t self.numero = n def print_type(self) : print(self.marque + " " + str(self.serie)) def print_specs(self) : self.print_type() print("Poids: " + str(self.poids) + " g") print("Taille: " + self.taille + " mm") def print(self) : self.print_specs() print(self.numero) def __eq__(self, other) : return (self.marque == other.marque) \ and (self.serie == other.serie) \ and (self.poids == other.poids) \ and (self.taille == other.taille) def __str__(self) : return self.marque + " " + str(self.serie) + "\n" \ + "Poids: " + str(self.poids) + " g" + "\n" \ + "Taille: " + self.taille + " mm" + "\n"
>>> phone_kim = NokiaPhone(5110,170,"132x48x31",number_kim) >>> phone_kim.print() Nokia 5110 Poids: 170 g Taille: 132x48x31 mm +32(0)10/479111 >>> phone_kim.print_specs() Nokia 5110 Poids: 170 g Taille: 132x48x31 mm >>> phone_kim.print_type() Nokia 5110 >>> print(phone_kim) Nokia 5110 Poids: 170 g Taille: 132x48x31 mm
Attention : phone_kim.print() ≠ print(phone_kim) ! Pourquoi ?
>>> number_charles = PhoneNumber("32","10","4","79222") >>> nokia_charles = NokiaPhone(7.1,160,"150x71x8",number_charles) >>> print(nokia_charles) Nokia 7.1 Poids: 160 g Taille: 150x71x8 mm >>> number_siegfried = PhoneNumber("32","10","4","79333") >>> nokia_siegfried = NokiaPhone(5110,170,"132x48x31",number_siegfried) >>> nokia_siegfried.print_specs() Nokia 5110 Poids: 170 g Taille: 132x48x31 mm >>> phone_charles = phone_kim >>> phone_charles.marque = "Alcatel" >>> phone_charles = None >>> print(phone_kim) Alcatel 5110 Poids: 170 g Taille: 132x48x31 mm >>> nokia_kim = NokiaPhone(5110,170,"132x48x31",number_kim) >>> nokia_siegfried = NokiaPhone(5110,170,"132x48x31",number_siegfried) >>> print(nokia_kim == nokia_siegfried) # appelera la méthode magique __eq__ True >>> print(nokia_kim is nokia_siegfried) # appelera la méthode magique __eq__ False >>> import copy >>> nokia_siegfried = copy.copy(nokia_kim) >>> nokia_kim.poids = 200 >>> print(nokia_kim) Nokia 5110 Poids: 200 g Taille: 132x48x31 mm >>> print(nokia_siegfried) Nokia 5110 Poids: 170 g Taille: 132x48x31 mm >>> kim_phone = NokiaPhone(5110,170,"132x48x31",number_kim) >>> siegfried_phone = copy.copy(kim_phone) >>> charles_phone = copy.deepcopy(kim_phone) >>> number_kim.postfix = "79000" >>> kim_phone.print() Nokia 5110 Poids: 170 g Taille: 132x48x31 mm +32(0)10/479000 >>> siegfried_phone.print() Nokia 5110 Poids: 170 g Taille: 132x48x31 mm +32(0)10/479000 >>> charles_phone.print() Nokia 5110 Poids: 170 g Taille: 132x48x31 mm +32(0)10/479111
Attention : le numéro de téléphone de Charles n'a pas changé (deep copy) mais le numéro de Siegried a changé (shallow copy).