Advertisement
Sergio_Istea

clases 3

Jun 22nd, 2023
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. # se define la clase
  2.  
  3. class Student:
  4.  
  5. # en este caso no hay parametro 'self', la palabra en si misma es uan convencion
  6. # pero puede usarse otra cosa como 'student'
  7.  
  8. # se definen los atributos
  9.  
  10. def __init__(student, name, subjects):
  11.  
  12.  
  13. if not isinstance(name, str):
  14. raise TypeError("name must be a string")
  15. elif not isinstance(subjects, list):
  16. raise TypeError("subjects must be a list")
  17. student.name = name
  18. student.subjects = subjects
  19.  
  20. # metodos
  21.  
  22. def print_info(student):
  23. print(f"Nombre: {student.name}")
  24. print(f"Materias: {student.subjects}")
  25.  
  26.  
  27. # end class
  28.  
  29. # Instancias
  30.  
  31. alumno_ana = Student("ana", ["biologia","matematica", "historia"])
  32. alumno_pablo = Student("pablo", ["biologia","ingles", "geografia"])
  33.  
  34.  
  35.  
  36. # llamdado a los metodos
  37.  
  38. alumno_ana.print_info()
  39.  
  40. alumno_pablo.print_info()
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement