Skip to content

Instantly share code, notes, and snippets.

@alejandrobernardis
Created November 28, 2014 00:59
Show Gist options
  • Save alejandrobernardis/b4b617b951d2732d2e5e to your computer and use it in GitHub Desktop.
Save alejandrobernardis/b4b617b951d2732d2e5e to your computer and use it in GitHub Desktop.
dir vs hasattr
i: a
True
r: False
True
---
i: b
True
r: ""
True
---
i: c
True
r: []
True
---
i: d
True
r: NotImplementedError()
False
---
i: e
True
r: None
False
---
import inspect
class X:
@property
def a(self):
print 'r: False'
return False
@property
def b(self):
print 'r: ""'
return ''
@property
def c(self):
print 'r: []'
return []
@property
def d(self):
print 'r: NotImplementedError()'
raise NotImplementedError()
@property
def e(self):
print 'r: None'
raise None
x = X()
for i in dir(x):
if i[0] != '_':
print 'i:', i
print i in dir(x)
print hasattr(x, i)
print '---'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment