Skip to content

Instantly share code, notes, and snippets.

@xiaoouwang
Created December 23, 2020 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaoouwang/8aff9b1ee36919874a5d6e4b5ee817ff to your computer and use it in GitHub Desktop.
Save xiaoouwang/8aff9b1ee36919874a5d6e4b5ee817ff to your computer and use it in GitHub Desktop.
Comprendre __str__ en Python (et l'avantage de __repr__)
# https://xiaoouwang.medium.com/comprendre-la-m%C3%A9thode-str-en-python-et-lavantage-de-repr-d40fceb833a1
# Author: Xiaoou Wang, Master's student in natural language processing looking for a phd position/contrat cifre. https://www.linkedin.com/in/xiaoou-wang/
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1) # <__main__.Animal object at 0x7f9060250410>
class Animal:
def __init__(self, color, age, breed):
self.color = color
self.age = age
self.breed = breed
def __str__(self):
return f"{self.color} {self.breed} of age {self.age}"
def __repr__(self):
return f"repr : {self.color} {self.breed} of age {self.age}"
a1 = Animal("Red", 36, "Dog")
a1 # repr : Red Dog of age 36
print(a1) # Red Dog of age 36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment