Enumerate Python class properties (not functions)

How do I enumerate functions of a Python class marked with @property?

class MyClass:
    def my_property():

Something like this, but there is no lambda filter for inspect.isproperty:

properties = inspect.getmembers(obj, inspect.isproperty)

Apparently, these are known as managed attributes.


This will do it:

inspect.getmembers(obj.__class__, lambda x: isinstance(x, property))

Here's how it works exactly (using IPython):

In [29]: class Foo(object):
   ....:     @property
   ....:     def foo(self): return 42

In [30]: obj = Foo()

In [31]: inspect.getmembers(obj.__class__, lambda prop: isinstance(prop, property))
Out[31]: [('foo', <property at 0x106aec6d8>)]

This works because property is a normal (new-style) class really; by marking something with @property, you're just making an instance of property. This also means that instances of properties (on a class) can be type-compared against property using isinstance.

Need Your Help

uploading an image through form

forms file-upload playframework image-uploading playframework-1.x

How should i write my controller class to upload an image through a form?

How can I make Stack call Happy, Alex and other build tools?

haskell happy haskell-stack

As part of my compiler, I need alex and happy to run as part of my build process. How does Stack support this scenario?