Created
October 4, 2021 08:33
-
-
Save FerdinaKusumah/7b2d4a23665e652472789a9ddc5e31bc to your computer and use it in GitHub Desktop.
Class Attributes as Instance Attributes
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 FooBar: | |
class_attr: str = "foo bar" | |
def __init__(self, instance_attr: str): | |
self.instance_attr = instance_attr | |
if __name__ == "__main__": | |
foo = FooBar("foo") | |
bar = FooBar("bar") | |
# get value from init fooBar class | |
print(foo.class_attr) | |
# set class attributes value related to foo class | |
foo.class_attr = "foo !!" | |
# print current class attr value, it will print "foo !!" | |
print(foo.class_attr) | |
# check bar class attr value, and for this time value is still "foo bar" | |
# this is weird ?? | |
print(bar.class_attr) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment