2011-02-01 59 views

回答

8

如果类,你实际上意味着你有实例,你可以简单地使用DIR:

a = list() 
print dir(a) 

如果你真正的意思看到您的类对象的方法:

a = list() 
print dir(a.__class__) 

注意,在这种情况下,双方将打印相同的结果,但是Python是相当动态的,你能想象附加新的方法,以一个实例,没有它被反映在课堂上。

如果您正在学习python并希望从其良好环境中的反射功能中受益,我建议您查看一下ipython。里面IPython中,你得到的方法制表完成/属性

+0

感谢。第一种情况就是我的意思,但知道两者都很好。 – 2011-02-01 13:56:20

0

说,它的名字是 “theobject”:DIR(theobject)

1

另一种方法,可以让你看看文档字符串对象是使用内置函数help()

>>> i = 1 
>>> help(type(i)) 
Help on class int in module __builtin__: 

class int(object) 
| int(x[, base]) -> integer 
| 
| Convert a string or number to an integer, if possible. A floating point 
| argument will be truncated towards zero (this does not include a string 
| representation of a floating point number!) When converting a string, use 
| the optional base. It is an error to supply a base when converting a 
| non-string. If base is zero, the proper base is guessed based on the 
| string content. If the argument is outside the integer range a 
| long object will be returned instead. 
| 
| Methods defined here: 
| 
| __abs__(...) 
|  x.__abs__() <==> abs(x) 
| 

(...等)。

0

如果你想制表完成,使用IPython中,或STDLIB的rlcompleter

>>> import rlcompleter 
>>> import readline 
>>> readline.parse_and_bind("tab: complete") 
>>> readline. <TAB PRESSED> 
readline.__doc__   readline.get_line_buffer( readline.read_init_file(
readline.__file__   readline.insert_text(  readline.set_completer(
readline.__name__   readline.parse_and_bind(
>>> readline.