Attribute Filtering

Yao Zuo edited this page Jul 28, 2018 · 6 revisions

pdir2 supports several filters:

properties: Find properties/variables defined in the inspected object.

methods: Find methods/functions defined in the inspected object.

public: Find public attributes.

own: Find attributes that are not inherited from parent classes.

These filters can be chained, order does NOT affect result. For example, use pdir(obj).public.own.methods or pdir(obj).own.methods.public to find all public own methods.

You can call search() on the returned results.

Example:

class Base(object):
    base_class_variable = 1

    def __init__(self):
        self.base_instance_variable = 2

    def base_method(self):
        pass


class DerivedClass(Base):
    derived_class_variable = 1

    def __init__(self):
        self.derived_instance_variable = 2
        super(DerivedClass, self).__init__()

    def derived_method(self):
        pass


inst = DerivedClass()

pdir(inst).properties  # 'base_class_variable', 'base_instance_variable',
                       # 'derived_class_variable', 'derived_instance_variable', '__class__',
                       # '__dict__', '__doc__', '__module__', '__weakref__'

pdir(inst).methods  # '__subclasshook__', '__delattr__', '__dir__', '__getattribute__',
                    # '__setattr__', '__init_subclass__', 'base_method',
                    # 'derived_method', '__format__', '__hash__', '__init__', '__new__',
                    # '__repr__', '__sizeof__', '__str__', '__reduce__', '__reduce_ex__',
                    # '__eq__', '__ge__', '__gt__', '__le__', '__lt__', '__ne__'

pdir(inst).public  # 'base_method', 'derived_method', 'base_class_variable',
                   # 'base_instance_variable', 'derived_class_variable',
                   # 'derived_instance_variable'

pdir(inst).own  # 'derived_method', '__init__', 'base_instance_variable',
                # 'derived_class_variable', 'derived_instance_variable', '__doc__',
                # '__module__'

pdir(inst).public.own.properties  # 'base_instance_variable','derived_class_variable',
                                  # 'derived_instance_variable'
pdir(inst).own.properties.public  # ditto
pdir(inst).properties.public.own  # ditto

pdir(inst).public.own.properties.search('derived_inst')  # 'derived_instance_variable'

You might wonder why base_instance_variable is in result of own? Because I'm not aware of any way to distinguish between properties initialized in child class's __init__ and parent class's __init__(assuming super() is called). So I just leave as it is. If you know a way, please let me know.

Note that these filters are designed for inspecting normal class/instance, they may not work as expected for other things like modules(yeah I know module is also an object). Please only use filters in REPL, DON'T rely on them in your program, or you might regret in the future.

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.