Last active
April 3, 2018 21:08
-
-
Save floft/78f55cdcb3780b3ddf802dcfe136b8ff to your computer and use it in GitHub Desktop.
Variable shared between classes
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
def f1(): | |
print("1 Class1.last_object", Class1.last_object) | |
Class1.last_object = "Class1 set last_object" | |
print("2 Class1.last_object", Class1.last_object) | |
class Class1(): | |
last_object = "base1" | |
def get(self): | |
return Class1.last_object | |
def f(self): | |
return f1() |
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
def f2(): | |
print("1 Class2.last_object", Class2.last_object) | |
Class2.last_object = "Class2 set last_object" | |
print("2 Class2.last_object", Class2.last_object) | |
class Class2(): | |
last_object = "base1" | |
def get(self): | |
return Class2.last_object | |
def f(self): | |
return f2() |
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
1 Class1.last_object base1 | |
2 Class1.last_object Class1 set last_object | |
Result: Class1 set last_object | |
1 Class2.last_object base1 | |
2 Class2.last_object Class2 set last_object | |
Result: Class2 set last_object | |
Final result: Class2 set last_object |
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
from class1 import Class1 | |
from class2 import Class2 | |
last_object = "base1" | |
sm = Class1() | |
sm.f() | |
print("Result:", sm.get()) | |
last_object = sm.get() | |
sm = Class2() | |
sm.f() | |
sm.get() | |
print("Result:", sm.get()) | |
last_object = sm.get() | |
print("Final result:", last_object) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment