[Tutor] Interesting problem

Kent Johnson kent37 at tds.net
Fri Jun 24 04:00:40 CEST 2005


Smith, Jeff wrote:
> That's what I was looking for.  Although I couldn't get the below to
> work, I went with a different mod of the original you gave:
> 
>     def get_props_as_dict(self):
>         d = dict()
>         for entry in dir(self.__class__):
>             if isinstance(getattr(self.__class__, entry), property):
>                 d[entry] = getattr(self, entry)
>         return d
> 

OK good! My code was untested and requires Python 2.4. I'm glad you could turn it into something that works for you.

Kent
> Thanks!
> Jeff
> 
> -----Original Message-----
> From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
> Behalf Of Kent Johnson
> OK, so will a variation on my last recipe work? This looks for property
> attributes of the class and gets the corresponding property on the
> instance:
>   def getprops_as_dict(self):
>     return dict(pname, getattr(self, pname) 
>       for pname in dir(self.__class__) 
>         if isinstance(getattr(self.__class__, pname), property))
>     )
> 
> Kent



More information about the Tutor mailing list