Created
January 29, 2015 14:55
-
-
Save zhester/1248bf28b8389868c20c to your computer and use it in GitHub Desktop.
Provides object iteration that filters out self-proclaimed "protected" 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 protected_iterator( object ): | |
""" | |
Provides object iteration that filters out self-proclaimed "protected" | |
attributes. | |
""" | |
#========================================================================= | |
def __init__( self, obj ): | |
""" | |
Initializes a protected_iterator object. | |
""" | |
self._obj = obj | |
#========================================================================= | |
def __iter__( self ): | |
""" | |
Provides iterator retrieval. | |
""" | |
# generates a list of filtered attributes for iteration | |
return [ | |
# resolve the host object's attribute for this name | |
getattr( self._obj, name ) | |
# pull the list of all attribute names | |
for name in self._obj.__dict__.iterkeys() | |
# ignore names that begin with underscores | |
if name[ 0 ] != '_' | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment