Created
August 22, 2019 07:54
-
-
Save forslund/b1c78d8d90e8e2212a866d69830120fe to your computer and use it in GitHub Desktop.
Python class variables test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class C: | |
class_var = None | |
def __init__(self): | |
if self.class_var is None: | |
self.class_var = [1] | |
print('Creating new instance') | |
else: | |
print('Appending to existing') | |
self.class_var.append(1) | |
c1 = C() | |
c2 = C() | |
c3 = C() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$python3 bad_class_var.py | |
Creating new instance | |
Creating new instance | |
Creating new instance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment