Python properties and string formatting

formatting, properties, python, string

Solution

`property` objects are descriptors. As such, they don't have any special abilities unless accessed through a class.

something like:

class Foo(object):
     @property
     def blah(self):
         return "Cheddar Cheese!"

a = Foo()
print('{a.blah}'.format(a=a))

should work. (You'll see `Cheddar Cheese!` printed)

Problem

I was under the impression python string formatting using .format() would correctly use properties, instead I get the default behaviour of an object being string-formatted: ``` >>> def get(): return "Blah" >>> a = property(get) >>> "{a}!".format(a=a) '<property object at 0x221df18>!' ``` Is this the intended behaviour, and if so what's a good way to implement a special behaviour for properties (eg, the above test would return "Blah!" instead)?

Original source

Related problems