Search code examples
pythonpython-2.7webapp2

Python: creating class instance


There is some part of code.

application = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/gbook', Guestbook)
])

As I understand it is a list of tuples:

[('/', MainPage), ('/gbook', Guestbook)]

Correct me please if I'm wrong.

And I have question: Where is obvious creation of instance of MainPage class and Guestbook?

Something like that: x = MainPage('/')

If this happens by this tuple ('/', MainPage), then my question: how it's happens?

I need some explanation.


Solution

  • The WSGIApplication it self creates the instances of the classes. In python you can pass classes around, just like you would pass instances of a class. For example:

    class A:
        def __init__(self):
            print "A Created"
    
    def foo(cls):
        inst = cls()
    
    foo(A)
    

    If you run this script it will print out "A Created" because you are passing in the class to foo which is creating a new instance from that class.