Latest |Kites |Pictures |Programming |Life
[filed under Programming]Python chain methods

I don't know why I didn't know this, but you can chain methods in Python. I use this quite a bit in PHP and Zend Framework uses it a little. But here it is in Python:

class table_model:
    def __init__(self):
        pass

    def connect(self):
        print 'connect to DB'
        return self
       
    def run_query(self):
        print 'run query'
        return self

    def get_last_result(self):
        return 'Hello'


table = table_model()
res = table.connect().run_query().get_last_result()
print res

>>>
connect to DB
run query
Hello

 

That's it. Job done.

14th of August, 2008@2:34:47 PM
add a comment, permanent link to article

Comments

Check this if you are a human being. Thanks. (I'm trying to reduce my comment spam :-)

Comment

Server Grind [0.0024 seconds]