xiº
xiº

Reputation: 4687

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.

Upvotes: 1

Views: 99

Answers (2)

Stephen Lin
Stephen Lin

Reputation: 4912

You can check source code of webapp2 to verify this.

In __init__ function of WSGIApplication , list '[('/', MainPage),('/gbook', Guestbook)]' will be passed as variable routes, then they will be instantiated by Route class.

Upvotes: 0

tantalum
tantalum

Reputation: 2451

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.

Upvotes: 3

Related Questions