[Tutor] Interesting problem

Jeremy Jones zanesdad at bellsouth.net
Thu Jun 23 20:21:58 CEST 2005


Smith, Jeff wrote:

>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?
>
>How might I go about this?
>
>Jeff
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>  
>
Like <some object>.__dict__?

Given this class:

      1 class Foo:
      2     def __init__(self, **kw):
      3         self.__dict__.update(kw)
      4


And creating an instance of it like this:

In [17]: foo = Foo(**{"bar":"b", "foo":"f", "bam":"bm"})


And accessing the properties of it like this:

In [18]: foo.foo
Out[18]: 'f'

In [19]: foo.bar
Out[19]: 'b'

In [20]: foo.bam
Out[20]: 'bm'


You can get all properties of it like this:

In [21]: foo.__dict__
Out[21]: {'bam': 'bm', 'bar': 'b', 'foo': 'f'}


Is this what you're looking for?

JJ


More information about the Tutor mailing list