Création d’une représentation ‘string’ d’une instance avec les méthodes _’_str__()’ et ‘__repr__()’

Author:

classe
{filelink=16117}

class Animation: #définition d'une classe 'Animation'
  """
   Ma documentation
  """
  def __init__(self, x=0, y=0): #Définition des variables d'instance
   self.point_x=x
   self.point_y=y

  def __str__(self):
   return "(%.2f %.2f)" %(self.point_x, self.point_y)

  def __repr__(self):
      return "Point(%.2f %.2f)" %(self.point_x,self.point_y)

anim=Animation(3, 6)
print str(anim)          #(3.00 6.00)
print repr(anim)         #Point(3.00 6.00)

Livres Sur ce Sujet

[amazon_image id=”2212134347″ link=”true” target=”_blank” size=”medium” ]Apprendre à programmer avec Python 3[/amazon_image] [amazon_image id=”B005J2L260″ link=”true” target=”_blank” size=”medium” ]Apprenez à programmer en Python[/amazon_image] [amazon_image id=”2100508830″ link=”true” target=”_blank” size=”medium” ]Python : Petit guide à l’usage du développeur agile[/amazon_image] [amazon_image id=”2212127081″ link=”true” target=”_blank” size=”medium” ]Apprendre à programmer avec Python 3 : Avec plus de 50 pages de corigés d’exercices ![/amazon_image]

Leave a Reply

Your email address will not be published. Required fields are marked *