2

This question may be a little green.

In the example below, I am trying to find out a list of the attribtues from the "message" parameter.

@respond_to("^meow")
def remind_me_at(self, message):
    fre = "asd: %s " % str(message.sender)
    fre = "asd: %s " % str(message.mention_name)
    fren = "asd: %s " % str(message.sender.name)
    #fren = "hello, %s!" % str(message)
    self.say(fren, message=message)
    self.say(fre, message=message)

As there is no documentation but the code is opensource, how can I find the file where the method is implemented efficiently; even if the class is in a library file.

[[Update]] I found this solution on another thread asking the question in a different way

[(name,type(getattr(math,name))) for name in dir(math)]
laycat
  • 5,381
  • 7
  • 31
  • 46

3 Answers3

2

Use the dir() built-in function. It returns a list of all attributes of an object.

print dir(message)
Rob
  • 3,418
  • 1
  • 19
  • 27
  • thank you I think its a good reply, could you teach us on how to separate attributes/function when using dir(). Additionally, I modified the question a little. – laycat Jun 23 '16 at 06:56
  • If I understood correctly you could know some information from `message`: `print(message.__class__)` would tell you the class to which a class instance belongs and `print(message.__module__)` would tell you the module where the class is available. I don't know if this is what you are looking for. – kikocorreoso Jun 23 '16 at 07:02
2

dir

dir([object]) -> list of strings

If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the 
attributes of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; 
otherwise the default dir() logic is used and returns:

for a module object: the module's attributes.
for a class object:  its attributes, and recursively the attributes
  of its bases.
for any other object: its attributes, its class's attributes, and
  recursively the attributes of its class's base classes.

Pleaes define a demo class called TestClass as follow.

>>> class TestClass(object):
...     def abc(self):
...         print('Hello ABC !')
...
...     def xyz(self):
...         print('Hello xyz !')
...
>>> dir(TestClass)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'abc', 'xyz']

getattr

getattr(object, name[, default]) -> value

Get a named attribute from an object;

getattr can get a attribute from TestClass:

>>> getattr(TestClass, 'abc')
<unbound method TestClass.abc>

>>> getattr(TestClass, 'xxx', 'Invalid-attr')
'Invalid-attr'

hasattr

hasattr(object, name) -> bool

Return whether the object has an attribute with the given name.

demo:

>>> hasattr(TestClass, 'abc')
True
>>> hasattr(TestClass, 'xxx')
False

If nothing can help you, please make your idea clear.

debug
  • 991
  • 10
  • 14
1

The dir command, as noted by Rob, is a able to list the attributes. This is handy when you're trying to debug running code.

Provided you can have an interactive session with the code (you know, just running the ol' REPL interpretor) then try:

>>> help(message)

If the developer of that library is a decent human, then hopefully they've written a worth-while docstring that'll give you some context about the object.

Another tool that's handy is the __file__ attribute, but this only works on modules. So if you know the name of the imported where you're getting message, you can do something like this:

>>> import some_module   # `message` comes from this module
>>> some_module.__file__
/usr/lib/python/python35/lib/site-packages/some_module/__init__.py

Once you know the directory, just start grepping for the source of message:

$ cd /usr/lib/python/python35/lib/site-packages/some_module
$ grep -r message *
willnx
  • 1,253
  • 1
  • 8
  • 14
  • thats what I was doing, but i think that the Message class that I was looking for is in a library. – laycat Jun 23 '16 at 08:12