Guest User

Untitled

a guest
Apr 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. class Foo:
  2. a = 5
  3.  
  4. def print_a(self):
  5. print(self.a)
  6.  
  7. class Foo:
  8. def __init__(self):
  9. self.a = 5
  10.  
  11. def print_a(self):
  12. print(self.a)
  13.  
  14. class Foo:
  15. a = [100] # Atributo de clase
  16.  
  17. def __init__(self, n):
  18. self.b = [n] # Atributo de instancia
  19.  
  20.  
  21. >>> inst1 = Foo(1)
  22. >>> inst2 = Foo(2)
  23.  
  24. >>> inst1.a
  25. [100]
  26. >>> inst2.a
  27. [100]
  28. >>> inst1.a.append(5)
  29. >>> inst1.a
  30. [100, 5]
  31. >>> inst2.a
  32. [100, 5]
  33.  
  34. >>> inst1.b
  35. [1]
  36. >>> inst2.b
  37. [2]
  38. >>> inst1.b.append(5)
  39. >>> inst1.b
  40. [1, 5]
  41. >>> inst2.b
  42. [2]
  43.  
  44. pi@rp1:~ $ python3
  45. Python 3.4.2 (default, Oct 19 2014, 13:31:11)
  46. [GCC 4.9.1] on linux
  47. Type "help", "copyright", "credits" or "license" for more information.
  48. >>> class Perro:
  49. ... numero_patas = 4
  50. ...
  51. >>> pancho = Perro()
  52. >>> fido = Perro()
  53. >>> pancho.numero_patas
  54. 4
  55. >>> fido.numero_patas
  56. 4
  57. >>> Perro.numero_patas
  58. 4
  59.  
  60. >>> fido.numero_patas = 3
  61. >>> fido.numero_patas
  62. 3
  63. >>>
  64.  
  65. >>> fido.cola = 'cortada'
  66. >>> fido.cola
  67. 'cortada'
  68.  
  69. >>> pancho.__dict__
  70. {}
  71. >>> fido.__dict__
  72. {'cola': 'cortada', 'numero_patas': 3}
  73. >>>
  74.  
  75. >>> pancho.__class__.__dict__
  76. mappingproxy({'__weakref__': <attribute '__weakref__' of 'Perro' objects>, '__dict__': <attribute '__dict__' of 'Perro' objects>, 'numero_patas': 4, '__module__': '__main__', '__doc__': None})
  77. >>> fido.__class__.__dict__
  78. mappingproxy({'__weakref__': <attribute '__weakref__' of 'Perro' objects>, '__dict__': <attribute '__dict__' of 'Perro' objects>, 'numero_patas': 4, '__module__': '__main__', '__doc__': None})
  79. >>>
Add Comment
Please, Sign In to add comment