From a54581a253e1dd6fc15996122052612b288c8fbf 2008-11-09 05:32:35 From: Fernando Perez Date: 2008-11-09 05:32:35 Subject: [PATCH] Implement 'k in s' more cleanly and add doctest. --- diff --git a/IPython/ipstruct.py b/IPython/ipstruct.py index b85e9d5..e3af2ee 100644 --- a/IPython/ipstruct.py +++ b/IPython/ipstruct.py @@ -163,8 +163,22 @@ class Struct: return self.__dict__[key] def __contains__(self,key): - """Allows use of the 'in' operator.""" - return self.__dict__.has_key(key) + """Allows use of the 'in' operator. + + Examples: + >>> s = Struct(x=1) + >>> 'x' in s + True + >>> 'y' in s + False + >>> s[4] = None + >>> 4 in s + True + >>> s.z = None + >>> 'z' in s + True + """ + return key in self.__dict__ def __iadd__(self,other): """S += S2 is a shorthand for S.merge(S2).""" @@ -246,12 +260,13 @@ class Struct: Optionally, one or more key=value pairs can be given at the end for direct update.""" - # The funny name __loc_data__ is to prevent a common variable name which - # could be a fieled of a Struct to collide with this parameter. The problem - # would arise if the function is called with a keyword with this same name - # that a user means to add as a Struct field. + # The funny name __loc_data__ is to prevent a common variable name + # which could be a fieled of a Struct to collide with this + # parameter. The problem would arise if the function is called with a + # keyword with this same name that a user means to add as a Struct + # field. newdict = Struct.__make_dict(self,__loc_data__,**kw) - for k,v in newdict.items(): + for k,v in newdict.iteritems(): self[k] = v def merge(self,__loc_data__=None,__conflict_solve=None,**kw):