Calling every method of an object from __init__

K.S.Sreeram sreeram at tachyontech.net
Mon Jun 19 16:18:21 EDT 2006


Rob Cowie wrote:
> class Foo(object):
>     def __init__(self):
>         #call all methods here
>     def test(self):
>         print 'The test method'
>     def hello(self):
>         print 'Hello user'

class Foo(object):
    def __init__(self):
        for k in dir(self) :
            if not k.startswith('__') :
                v = getattr( self, k )
                v()
    def test(self):
        print 'The test method'
    def hello(self):
        print 'Hello user'


you can also add a test for 'if callable(v) :' to the for loop if you
have data fields in Foo.

Regards
Sreeram

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 260 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20060620/f089ec52/attachment.sig>


More information about the Python-list mailing list