Created
          November 28, 2014 00:59 
        
      - 
      
- 
        Save alejandrobernardis/b4b617b951d2732d2e5e to your computer and use it in GitHub Desktop. 
    dir vs hasattr
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | 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 | |
| --- | 
  
    
      This file contains hidden or 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
    
  
  
    
  | 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