Skip to content

Instantly share code, notes, and snippets.

@FerdinaKusumah
Created October 4, 2021 08:33
Show Gist options
  • Save FerdinaKusumah/7b2d4a23665e652472789a9ddc5e31bc to your computer and use it in GitHub Desktop.
Save FerdinaKusumah/7b2d4a23665e652472789a9ddc5e31bc to your computer and use it in GitHub Desktop.
Class Attributes as Instance Attributes
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