Add Properties to Instances?

Martin Miller ggrp1.20.martineau at dfgh.net
Sat Mar 12 12:48:42 EST 2005


I'm trying to create some read-only instance specific properties, but
the following attempt didn't work:

> class Foobar(object):
>     pass
>
> foobar = Foobar()
> foobar.x = property(fget=lambda: 42)
>
> print "foobar.x:", foobar.x

Which results in the following ouput instead of '42':
  foobar.x: <property object at 0x00AE57B0>

I also tried this:
> foobar.__dict__['x'] = property(fget=lambda: 42)
but get the same behavior.

Can anyone tell me what's wrong with this approach (and perhaps the
correct way to do it, if there is one)?

TIA,
Martin




More information about the Python-list mailing list