view-callable class - request arg receives context object

64 views
Skip to first unread message

Simon Yarde

unread,
Oct 31, 2012, 12:02:19 PM10/31/12
to pylons-...@googlegroups.com

Docs say you should expect to handle a request argument in a view-callable class.

http://pyramid.readthedocs.org/en/latest/narr/views.html#defining-a-view-callable-as-a-class

But I found I was getting a context object for the request arg.

Does this mean context and request are being passed (in that order) as positional args, rather can keywords?

I know I can just reverse the order so context is first - just asking if there is anything wrong with below code that would cause this, or this is view-callables are called this way for any reason?


class SomeView():
"""
"""
def __init__(self,
request, # called positionally?
context, # ..
*args,
**kwargs
):
print('"request" is {0}'.format(request.__class__))
print('"context" is {0}'.format(context.__class__))


"request" is <class 'pyramid.traversal.DefaultRootFactory'>
"context" is <class 'pyramid.util.Request'>

Michael Merickel

unread,
Oct 31, 2012, 12:06:54 PM10/31/12
to pylons-...@googlegroups.com
They are called positionally via introspection and not by name. The
calling convention is here:

http://pyramid.readthedocs.org/en/latest/narr/views.html#alternate-view-callable-argument-calling-conventions
> --
> You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
> To post to this group, send email to pylons-...@googlegroups.com.
> To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
>

Simon

unread,
Oct 31, 2012, 1:07:24 PM10/31/12
to pylons-...@googlegroups.com
Thanks Michael

Is the example below given in #defining-a-view-callable-as-a-class incorrect?

I.e. ``request`` is a context-object because it is the first-position arg.


from pyramid.response import Response

class MyView(object):
    def __init__(self, request):
        self.request = request

    def __call__(self):
        return Response('hello')

Michael Merickel

unread,
Oct 31, 2012, 1:12:09 PM10/31/12
to pylons-...@googlegroups.com
No, it's correct. As I said it uses introspection to see what args are
there. If your function takes 1 arg, it will be the request. If it
takes 2 or more args the first two will be context and request in that
order. You need to be aware of this if you try to do (request, *args)
because the first arg will be context and args[0] will be request.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/pylons-discuss/-/XfRjxah4XjcJ.

Simon Yarde

unread,
Oct 31, 2012, 1:26:34 PM10/31/12
to pylons-...@googlegroups.com
Understood - thanks for clarifying. Best, S
Reply all
Reply to author
Forward
0 new messages