list of 'magic methods' or builtin class methods... want to excludethose from dir output

Discussion in 'Python' started by DG, Jul 22, 2009.

  1. DG

    DG Guest

    There is probably a better way to do this (please enlighten me, if you
    know), but what I want to do is get a list of a class' attributes
    minus whatever the 'builtin' methods are. I would also like to do
    this for instances of classes as well. I don't want to use __dict__
    because I want to get all of the attributes that have been added
    throughout the inheritance tree, just not the builtin ones.

    I was wondering if there is a function to return a list of these, so I
    could perform a 'dir' on the object (class or instance) and filter out
    the builtin attributes. I know I could create my own list something
    like
    ['__class__', '__delattr__', ..., '__weakref__'] and use that. I
    would see this being fragile to Python version differences as new ones
    are added or taken away.

    A more flexible way is to have the list be filled out dynamically by
    creating a 'junk' class that inherits only from object and storing
    it's dir() output into the filter list. This is probably what I'll do
    unless someone knows of a builtin function that will give me a
    (version-safe) list.

    Thanks!
     
    DG, Jul 22, 2009
    #1
    1. Advertising

  2. DG

    DG Guest

    Re: list of 'magic methods' or builtin class methods... want toexclude those from dir output

    On Jul 22, 12:15 pm, DG <> wrote:
    > There is probably a better way to do this (please enlighten me, if you
    > know), but what I want to do is get a list of a class' attributes
    > minus whatever the 'builtin' methods are.  I would also like to do
    > this for instances of classes as well.  I don't want to use __dict__
    > because I want to get all of the attributes that have been added
    > throughout the inheritance tree, just not the builtin ones.
    >
    > I was wondering if there is a function to return a list of these, so I
    > could perform a 'dir' on the object (class or instance) and filter out
    > the builtin attributes.  I know I could create my own list something
    > like
    > ['__class__', '__delattr__', ..., '__weakref__'] and use that.  I
    > would see this being fragile to Python version differences as new ones
    > are added or taken away.
    >
    > A more flexible way is to have the list be filled out dynamically by
    > creating a 'junk' class that inherits only from object and storing
    > it's dir() output into the filter list.  This is probably what I'll do
    > unless someone knows of a builtin function that will give me a
    > (version-safe) list.
    >
    > Thanks!


    Haha, replying to my own post. Here is the implementation of my idea
    of dynamically creating the filter lists from a 'junk' object. As of
    now it appears that the dir output of a class is the same as the dir
    output of an instance, but if that ever changes, this should future
    proof it. This is only a 4-liner, so it is probably good enough for
    what I want to do, but I would rather use a builtin/standard way if
    there is one.

    class A(object):
    pass
    class_filter_methods = dir(A)
    instance_filter_methods = dir(A())
     
    DG, Jul 22, 2009
    #2
    1. Advertising

  3. DG

    DG Guest

    Re: list of 'magic methods' or builtin class methods... want toexclude those from dir output

    On Jul 22, 12:24 pm, DG <> wrote:
    > On Jul 22, 12:15 pm, DG <> wrote:
    >
    >
    >
    > > There is probably a better way to do this (please enlighten me, if you
    > > know), but what I want to do is get a list of a class' attributes
    > > minus whatever the 'builtin' methods are.  I would also like to do
    > > this for instances of classes as well.  I don't want to use __dict__
    > > because I want to get all of the attributes that have been added
    > > throughout the inheritance tree, just not the builtin ones.

    >
    > > I was wondering if there is a function to return a list of these, so I
    > > could perform a 'dir' on the object (class or instance) and filter out
    > > the builtin attributes.  I know I could create my own list something
    > > like
    > > ['__class__', '__delattr__', ..., '__weakref__'] and use that.  I
    > > would see this being fragile to Python version differences as new ones
    > > are added or taken away.

    >
    > > A more flexible way is to have the list be filled out dynamically by
    > > creating a 'junk' class that inherits only from object and storing
    > > it's dir() output into the filter list.  This is probably what I'll do
    > > unless someone knows of a builtin function that will give me a
    > > (version-safe) list.

    >
    > > Thanks!

    >
    > Haha, replying to my own post.  Here is the implementation of my idea
    > of dynamically creating the filter lists from a 'junk' object.  As of
    > now it appears that the dir output of a class is the same as the dir
    > output of an instance, but if that ever changes, this should future
    > proof it.  This is only a 4-liner, so it is probably good enough for
    > what I want to do, but I would rather use a builtin/standard way if
    > there is one.
    >
    > class A(object):
    >     pass
    > class_filter_methods = dir(A)
    > instance_filter_methods = dir(A())


    Wow, I'm a quick one. Sharp as a marble. 2-liner:

    class_filter_methods = dir(object)
    instance_filter_methods = dir(object())
     
    DG, Jul 22, 2009
    #3
  4. DG

    Terry Reedy Guest

    Re: list of 'magic methods' or builtin class methods... want toexclude those from dir output

    DG wrote:
    > There is probably a better way to do this (please enlighten me, if you
    > know), but what I want to do is get a list of a class' attributes
    > minus whatever the 'builtin' methods are. I would also like to do
    > this for instances of classes as well. I don't want to use __dict__
    > because I want to get all of the attributes that have been added
    > throughout the inheritance tree, just not the builtin ones.
    >
    > I was wondering if there is a function to return a list of these, so I
    > could perform a 'dir' on the object (class or instance) and filter out
    > the builtin attributes. I know I could create my own list something
    > like
    > ['__class__', '__delattr__', ..., '__weakref__'] and use that. I
    > would see this being fragile to Python version differences as new ones
    > are added or taken away.
    >
    > A more flexible way is to have the list be filled out dynamically by
    > creating a 'junk' class that inherits only from object and storing
    > it's dir() output into the filter list. This is probably what I'll do
    > unless someone knows of a builtin function that will give me a
    > (version-safe) list.
    >
    > Thanks!

    3.1
    >>> list(filter(lambda s: s[0:2]!='__', dir(object)))

    []

    Now substitute your object for lambda. This assumes of course that you
    are doing no name-mangling __xxx names.

    tjr
     
    Terry Reedy, Jul 22, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michalis Giannakidis
    Replies:
    0
    Views:
    282
    Michalis Giannakidis
    Nov 26, 2006
  2. Michalis Giannakidis
    Replies:
    9
    Views:
    322
    Fredrik Lundh
    Nov 28, 2006
  3. bdb112
    Replies:
    2
    Views:
    309
    Chris Torek
    Jul 2, 2011
  4. Giles Bowkett
    Replies:
    9
    Views:
    416
    Giles Bowkett
    Dec 17, 2007
  5. Kga Agk
    Replies:
    2
    Views:
    155
    Kga Agk
    Jun 29, 2009
Loading...

Share This Page