2016-01-17 109 views
-1

打印方法的名稱,例如:我怎樣才能在我的課

class Employee: 
    'Common base class for all employees' 
    empCount = 0 

    def __init__(self, name, salary): 
     self.name = name 
     self.salary = salary 
     Employee.empCount += 1 

    def displayCount(self): 
    print "Total Employee %d" % Employee.empCount 

    def displayEmployee(self): 
     print "Name : ", self.name, ", Salary: ", self.salary 

我想打印的方法名。 輸出應該是這樣的:

Class Employee contains (displayCount,displayEmployee) 
+0

您使用哪種編程語言? – halfzebra

回答

1

這得到所有方法名稱:

import inspect 
methodnames = [name for name,member in inspect.getmembers(Employee) if inspect.ismethod(member)] 

...這不相同,但不包括像__init__方法名與__開始:

import inspect 
methodnames = [name for name,member in inspect.getmembers(Employee) if inspect.ismethod(member) and not name.startswith('__')] 

在第二個版本中,methodnames將是['displayCount', 'displayEmployee']