Instantly share code, notes, and snippets.
weaming/shared-class-variable-references.py
Last active Jul 27, 2020
#!/usr/bin/env python3 | |
class C: | |
a = 1 | |
def __init__(self): | |
self.a += 1 | |
def update(self): | |
self.a += 1 | |
print(C.a) | |
c = C() | |
print(c.a) | |
c.update() | |
print(c.a) | |
print('---') | |
print(C.a) | |
c = C() | |
print(c.a) | |
c.update() | |
print(c.a) | |
print('---' * 2) | |
class C: | |
a = [] | |
def __init__(self): | |
self.a.append(1) | |
def update(self): | |
self.a.append(1) | |
print(C.a) | |
c = C() | |
print(c.a) | |
c.update() | |
print(c.a) | |
print('---') | |
print(C.a) | |
c = C() | |
print(c.a) | |
c.update() | |
print(c.a) | |
print('---' * 2) | |
class C: | |
a = [] | |
def __init__(self): | |
self.a = [] | |
self.a.append(1) | |
def update(self): | |
self.a.append(1) | |
print(C.a) | |
c = C() | |
print(c.a) | |
c.update() | |
print(c.a) | |
print('---') | |
print(C.a) | |
c = C() | |
print(c.a) | |
c.update() | |
print(c.a) | |
''' | |
> python3 x.py | |
1 | |
2 | |
3 | |
--- | |
1 | |
2 | |
3 | |
------ | |
[] | |
[1] | |
[1, 1] | |
--- | |
[1, 1] | |
[1, 1, 1] | |
[1, 1, 1, 1] | |
------ | |
[] | |
[1] | |
[1, 1] | |
--- | |
[] | |
[1] | |
[1, 1] | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment