Skip to content

Instantly share code, notes, and snippets.

@rm-you
Created December 8, 2017 21:40
Show Gist options
  • Save rm-you/14fe2b0dc06e86bde4e685ed0d0d3ea8 to your computer and use it in GitHub Desktop.
Save rm-you/14fe2b0dc06e86bde4e685ed0d0d3ea8 to your computer and use it in GitHub Desktop.
class MyClass(object):
_myvar = None
class MyOtherClass(object):
pass
a = MyClass()
b = MyOtherClass()
a._myvar
-> None
b._myvar
-> Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'MyOtherClass' object has no attribute '_myvar'
dir(a)
-> ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_myvar']
dir(b)
-> ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
a._myvar = 1
a._myvar
-> 1
MyClass._myvar
-> None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment