Rob Cowie wrote: > Hi all, > > Is there a simple way to call every method of an object from its > __init__()? > > For example, given the following class, what would I replace the > comment line in __init__() with to result in both methods being called? > I understand that I could just call each method by name but I'm looking > for a mechanism to avoid this. > > class Foo(object): > def __init__(self): > #call all methods here > def test(self): > print 'The test method' > def hello(self): > print 'Hello user' > > Thanks, > > Rob C
First off, calling *every* method of an object is most likely a bad idea. A class has special methods defined automatically and you probably don't intend calling those. The way to go is have a naming convention for the methods you want to call, e.g. methods starting with "dump_". In this case you could use the inspect module to pick the object's methods and filter out the irrelevant methods: import inspect class Foo(object): def __init__(self): for name,method in inspect.getmembers(self,inspect.ismethod): if name.startswith('dump_'): method() def dump_f(self): print 'The test method' def dump_g(self): print 'Hello user' if __name__ == '__main__': Foo() George -- http://mail.python.org/mailman/listinfo/python-list