1

For context, I will use the example that prompted this question which is the DNA sequence class of Scikit-Bio.

The base class is a generic python sequence class. A Sequence class inherits from that class for sequences that are specifically nucleic acids (DNA, RNA ...). Finally, there is a DNA class that inherits from Sequence that enforces the specific alphabet of DNA.

So the following codes lists all the attributes of a DNA object.

from skbio import DNA

d = DNA('ACTGACTG')
for attr in dir(d):
    # All the attributes of d.

How can I find which parent class each attribute belongs to? The reason why I am interested in this is that I am looking through the source code, and I want to be able to know which file I can find each method that I want to look at.

The best I can think of is something like this:

for attr in dir(d)
    print type(attr)

But this just returns all string types (I guess dir() returns a list of strings).

How can I achieve this in python? Is there an inherent reason to not attemps this at all? Or is this something that comes up often in OOP?

Malonge
  • 1,980
  • 5
  • 23
  • 33
  • 1
    The `__mro__` attribute of a class is a tuple of its bases, in **m**ethod **r**esolution **o**rder, if that's what you're after. You can find the file for a class as follows: http://stackoverflow.com/a/697395/3001761 – jonrsharpe Oct 01 '15 at 19:53
  • 1
    You are probably better off grepping once you have a list of things you are interested in. – C.B. Oct 01 '15 at 19:54
  • Possible duplicate of: http://stackoverflow.com/questions/961048/get-class-that-defined-method – Kamil Sokołowski Oct 01 '15 at 19:55
  • @KamilSokołowski not quite, that's how to get the file once you have the class itself – jonrsharpe Oct 01 '15 at 19:55
  • 1
    For your final code fragment, try `for attr in dir(d): print type(getattr(d, attr))`. – Robᵩ Oct 01 '15 at 20:03
  • @Robᵩ that worked, and it further illustrated your answer because all of the methods are of type 'instancemethod'. – Malonge Oct 01 '15 at 20:05

1 Answers1

1

Attributes don't typically belong to any class. Attributes typically belong to the object of which they are an attribute.

Methods, however, relate intimately to the class in which they are defined.

Consider this program:

class base(object):
    def create_attrib_a(self):
        self.a = 1
class derived(base):
    def create_attrib_b(self):
        self.b = 1
def create_attrib_c(obj):
   obj.c = 1

import inspect

o = derived()
o.create_attrib_a()
o.create_attrib_b()
create_attrib_c(o)
o.d = 1

# The objects attributes are relatively anonymous
print o.__dict__

# But the class's methods have lots of information available
for name, value in inspect.getmembers(o, inspect.ismethod):
    print 'Method=%s, filename=%s, line number=%d'%(
        name,
        value.im_func.func_code.co_filename,
        value.im_func.func_code.co_firstlineno)

As you can see, each of the attributes a, b, c and d are associated with the object bound to o. None of them relate, in any technical sense, to any specific class.

However, the methods create_attrib_a and create_attrib_b carry precisely the information you desire. See how the inspect module can retrieve the filename and line number of their definition.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Interesting so there is no way to find out which class "create_attrib_a" was defined in just given the object? – Malonge Oct 01 '15 at 20:03
  • There is no (practical) way to find which class's code defined `a`. There are ways to find the class that defined `create_attrib_a`. Methods (like `create_attrib_a` have more data available than attributes (like `a`). Are you saying that you are interested in the methods? – Robᵩ Oct 01 '15 at 20:05
  • @Malonge , see my recent edit for how to find the source code for the methods. – Robᵩ Oct 01 '15 at 20:17
  • Thanks for helping me out with this! – Malonge Oct 01 '15 at 20:54