Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Foo:
- a = 5
- def print_a(self):
- print(self.a)
- class Foo:
- def __init__(self):
- self.a = 5
- def print_a(self):
- print(self.a)
- class Foo:
- a = [100] # Atributo de clase
- def __init__(self, n):
- self.b = [n] # Atributo de instancia
- >>> inst1 = Foo(1)
- >>> inst2 = Foo(2)
- >>> inst1.a
- [100]
- >>> inst2.a
- [100]
- >>> inst1.a.append(5)
- >>> inst1.a
- [100, 5]
- >>> inst2.a
- [100, 5]
- >>> inst1.b
- [1]
- >>> inst2.b
- [2]
- >>> inst1.b.append(5)
- >>> inst1.b
- [1, 5]
- >>> inst2.b
- [2]
- pi@rp1:~ $ python3
- Python 3.4.2 (default, Oct 19 2014, 13:31:11)
- [GCC 4.9.1] on linux
- Type "help", "copyright", "credits" or "license" for more information.
- >>> class Perro:
- ... numero_patas = 4
- ...
- >>> pancho = Perro()
- >>> fido = Perro()
- >>> pancho.numero_patas
- 4
- >>> fido.numero_patas
- 4
- >>> Perro.numero_patas
- 4
- >>> fido.numero_patas = 3
- >>> fido.numero_patas
- 3
- >>>
- >>> fido.cola = 'cortada'
- >>> fido.cola
- 'cortada'
- >>> pancho.__dict__
- {}
- >>> fido.__dict__
- {'cola': 'cortada', 'numero_patas': 3}
- >>>
- >>> pancho.__class__.__dict__
- mappingproxy({'__weakref__': <attribute '__weakref__' of 'Perro' objects>, '__dict__': <attribute '__dict__' of 'Perro' objects>, 'numero_patas': 4, '__module__': '__main__', '__doc__': None})
- >>> fido.__class__.__dict__
- mappingproxy({'__weakref__': <attribute '__weakref__' of 'Perro' objects>, '__dict__': <attribute '__dict__' of 'Perro' objects>, 'numero_patas': 4, '__module__': '__main__', '__doc__': None})
- >>>
Add Comment
Please, Sign In to add comment