Quick Filter Dictionary
abcd
codecraig at gmail.com
Wed Mar 14 07:29:05 EDT 2007
Hi,
I have a dictionary which may contain various keys/values, however,
it will always contain 'name' and 'age' keys.
This dictionary is kept inside a class, such as....
class Person:
def __init__(self, name, age):
self.data = {'name' : name, 'age' : age}
def getData(self, includeNameAge=False):
if includeNameAge: return self.data
tmp = <shallow or deep copy of 'data'>
del tmp['name']
del tmp['age']
return tmp
The getdata method should return the data stored for the
person, however, it should be able to return the data excluding the
'name' and 'age'. Any suggestions on how to best implement this?
Keep in mind this sample code is meant to be an example, I realize
that a person would probably always want the name and age returned.
thanks
More information about the Python-list
mailing list