61a lecture 16 announcements string representations
play

61A Lecture 16 Announcements String Representations String - PowerPoint PPT Presentation

61A Lecture 16 Announcements String Representations String Representations 4 String Representations An object value should behave like the kind of data it is meant to represent 4 String Representations An object value should behave like the


  1. 61A Lecture 16

  2. Announcements

  3. String Representations

  4. String Representations 4

  5. String Representations An object value should behave like the kind of data it is meant to represent 4

  6. String Representations An object value should behave like the kind of data it is meant to represent For instance, by producing a string representation of itself 4

  7. String Representations An object value should behave like the kind of data it is meant to represent For instance, by producing a string representation of itself Strings are important: they represent language and programs 4

  8. String Representations An object value should behave like the kind of data it is meant to represent For instance, by producing a string representation of itself Strings are important: they represent language and programs In Python, all objects produce two string representations: 4

  9. String Representations An object value should behave like the kind of data it is meant to represent For instance, by producing a string representation of itself Strings are important: they represent language and programs In Python, all objects produce two string representations: • The str is legible to humans 4

  10. String Representations An object value should behave like the kind of data it is meant to represent For instance, by producing a string representation of itself Strings are important: they represent language and programs In Python, all objects produce two string representations: • The str is legible to humans • The repr is legible to the Python interpreter 4

  11. String Representations An object value should behave like the kind of data it is meant to represent For instance, by producing a string representation of itself Strings are important: they represent language and programs In Python, all objects produce two string representations: • The str is legible to humans • The repr is legible to the Python interpreter The str and repr strings are often the same, but not always 4

  12. The repr String for an Object 5

  13. The repr String for an Object The repr function returns a Python expression (a string) that evaluates to an equal object 5

  14. The repr String for an Object The repr function returns a Python expression (a string) that evaluates to an equal object repr(object) -> string Return the canonical string representation of the object. For most object types, eval(repr(object)) == object. 5

  15. The repr String for an Object The repr function returns a Python expression (a string) that evaluates to an equal object repr(object) -> string Return the canonical string representation of the object. For most object types, eval(repr(object)) == object. The result of calling repr on a value is what Python prints in an interactive session 5

  16. The repr String for an Object The repr function returns a Python expression (a string) that evaluates to an equal object repr(object) -> string Return the canonical string representation of the object. For most object types, eval(repr(object)) == object. The result of calling repr on a value is what Python prints in an interactive session >>> 12e12 5

  17. The repr String for an Object The repr function returns a Python expression (a string) that evaluates to an equal object repr(object) -> string Return the canonical string representation of the object. For most object types, eval(repr(object)) == object. The result of calling repr on a value is what Python prints in an interactive session >>> 12e12 12000000000000.0 5

  18. The repr String for an Object The repr function returns a Python expression (a string) that evaluates to an equal object repr(object) -> string Return the canonical string representation of the object. For most object types, eval(repr(object)) == object. The result of calling repr on a value is what Python prints in an interactive session >>> 12e12 12000000000000.0 >>> print(repr(12e12)) 5

  19. The repr String for an Object The repr function returns a Python expression (a string) that evaluates to an equal object repr(object) -> string Return the canonical string representation of the object. For most object types, eval(repr(object)) == object. The result of calling repr on a value is what Python prints in an interactive session >>> 12e12 12000000000000.0 >>> print(repr(12e12)) 12000000000000.0 5

  20. The repr String for an Object The repr function returns a Python expression (a string) that evaluates to an equal object repr(object) -> string Return the canonical string representation of the object. For most object types, eval(repr(object)) == object. The result of calling repr on a value is what Python prints in an interactive session >>> 12e12 12000000000000.0 >>> print(repr(12e12)) 12000000000000.0 Some objects do not have a simple Python-readable string 5

  21. The repr String for an Object The repr function returns a Python expression (a string) that evaluates to an equal object repr(object) -> string Return the canonical string representation of the object. For most object types, eval(repr(object)) == object. The result of calling repr on a value is what Python prints in an interactive session >>> 12e12 12000000000000.0 >>> print(repr(12e12)) 12000000000000.0 Some objects do not have a simple Python-readable string >>> repr(min) '<built-in function min>' 5

  22. The str String for an Object Human interpretable strings are useful as well: 6

  23. The str String for an Object Human interpretable strings are useful as well: >>> from fractions import Fraction 6

  24. The str String for an Object Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2) 6

  25. The str String for an Object Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2) >>> repr(half) 6

  26. The str String for an Object Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2) >>> repr(half) 'Fraction(1, 2)' 6

  27. The str String for an Object Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2) >>> repr(half) 'Fraction(1, 2)' >>> str(half) 6

  28. The str String for an Object Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2) >>> repr(half) 'Fraction(1, 2)' >>> str(half) '1/2' 6

  29. The str String for an Object Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2) >>> repr(half) 'Fraction(1, 2)' >>> str(half) '1/2' The result of calling str on the value of an expression is what Python prints using the print function: 6

  30. The str String for an Object Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2) >>> repr(half) 'Fraction(1, 2)' >>> str(half) '1/2' The result of calling str on the value of an expression is what Python prints using the print function: >>> print(half) 6

  31. The str String for an Object Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2) >>> repr(half) 'Fraction(1, 2)' >>> str(half) '1/2' The result of calling str on the value of an expression is what Python prints using the print function: >>> print(half) 1/2 6

  32. The str String for an Object Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2) >>> repr(half) 'Fraction(1, 2)' >>> str(half) '1/2' The result of calling str on the value of an expression is what Python prints using the print function: >>> print(half) 1/2 (Demo) 6

  33. Polymorphic Functions

  34. Polymorphic Functions 8

  35. Polymorphic Functions Polymorphic function: A function that applies to many (poly) different forms (morph) of data 8

  36. Polymorphic Functions Polymorphic function: A function that applies to many (poly) different forms (morph) of data str and repr are both polymorphic; they apply to any object 8

  37. Polymorphic Functions Polymorphic function: A function that applies to many (poly) different forms (morph) of data str and repr are both polymorphic; they apply to any object repr invokes a zero-argument method __repr__ on its argument 8

  38. Polymorphic Functions Polymorphic function: A function that applies to many (poly) different forms (morph) of data str and repr are both polymorphic; they apply to any object repr invokes a zero-argument method __repr__ on its argument >>> half.__repr__() 'Fraction(1, 2)' 8

  39. Polymorphic Functions Polymorphic function: A function that applies to many (poly) different forms (morph) of data str and repr are both polymorphic; they apply to any object repr invokes a zero-argument method __repr__ on its argument >>> half.__repr__() 'Fraction(1, 2)' str invokes a zero-argument method __str__ on its argument 8

Recommend


More recommend