Skip to content

Instantly share code, notes, and snippets.

@zhester
Created January 29, 2015 14:55
Show Gist options
  • Save zhester/1248bf28b8389868c20c to your computer and use it in GitHub Desktop.
Save zhester/1248bf28b8389868c20c to your computer and use it in GitHub Desktop.
Provides object iteration that filters out self-proclaimed "protected" attributes.
#=============================================================================
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