[Tutor] Interesting problem

Christian Wyglendowski Christian.Wyglendowski at greenville.edu
Thu Jun 23 20:24:30 CEST 2005


 

> -----Original Message-----
> 
> Consider a class with a lt of properties.  I would like a member
> function which generates a dictionary where the keys are the property
> names and the values are the property values?
> 
> Is this clear?

I think so :-)

> How might I go about this?

I think you could simply use the special object attribute __dict__.  It
holds a dictionary of all properties and their values.

>>> class Something:
... 	def __init__(self, item1, item2, item3, item4, item5):
... 		self.item1 = item1
... 		self.item2 = item2
... 		self.item3 = item3
... 		self.item4 = item4
... 		self.item5 = item5
... 		
>>> s = Something(42,52,55,1,54)
>>> s.__dict__
{'item2': 52, 'item3': 55, 'item1': 42, 'item4': 1, 'item5': 54}


> Jeff

HTH,

Christian
http://www.dowski.com


More information about the Tutor mailing list