Reputation: 15082
This is more of a general Python question than a Flask question.
This snippet of code comes from https://github.com/mitsuhiko/flask/blob/master/flask/views.py#L18.
@classmethod
def as_view(cls, name, *class_args, **class_kwargs):
"""Converts the class into an actual view function that can be used
with the routing system. Internally this generates a function on the
fly which will instantiate the :class:`View` on each request and call
the :meth:`dispatch_request` method on it.
The arguments passed to :meth:`as_view` are forwarded to the
constructor of the class.
"""
def view(*args, **kwargs):
self = view.view_class(*class_args, **class_kwargs)
return self.dispatch_request(*args, **kwargs)
if cls.decorators:
view.__name__ = name
view.__module__ = cls.__module__
for decorator in cls.decorators:
view = decorator(view)
# we attach the view class to the view function for two reasons:
# first of all it allows us to easily figure out what class-based
# view this thing came from, secondly it's also used for instantiating
# the view class so you can actually replace it with something else
# for testing purposes and debugging.
view.view_class = cls
view.__name__ = name
view.__doc__ = cls.__doc__
view.__module__ = cls.__module__
view.methods = cls.methods
return view
Can somebody explain to me how exactly this as_view()
function forwards its arguments to the constructor of the View
class as it says it does in the method comment?
If not a direct explanation, maybe a shove in the right direction about specifically what I need to learn about Python-wise to better understand what is going on.
Thanks
Upvotes: 2
Views: 767
Reputation: 599550
This line is the key one:
self = view.view_class(*class_args, **class_kwargs)
It's taking the arguments passed to as_view
and using them to create an instance of view
with those arguments.
Upvotes: 3