【发布时间】:2014-04-25 11:10:15
【问题描述】:
view.generic.base中view的django中有类定义
class View(object):
...
def as_view(cls, **initkwargs):
"""
Main entry point for a request-response process.
"""
...
def view(request, *args, **kwargs):
self = cls(**initkwargs)
if hasattr(self, 'get') and not hasattr(self, 'head'):
self.head = self.get
self.request = request
self.args = args
self.kwargs = kwargs
return self.dispatch(request, *args, **kwargs)
# take name and docstring from class
update_wrapper(view, cls, updated=())
# and possible attributes set by decorators
# like csrf_exempt from dispatch
update_wrapper(view, cls.dispatch, assigned=())
return view
它应该返回一个类似 view(request, *args, **kwargs) 的函数。 请注意,'def view' 中有一个变量,即 'cls'。
假设我们运行:
tmp = View.as_view(),tmp(request, *args, **kwargs) 怎么知道cls的值是多少?
这是简化的情况!!!!!
这样说python代码:
>>> def func1(a):
... def func2(b):
... c = a + b
... print c
... return func2
...
>>> func3 = func1(3)
>>> func3(1)
4
>>> func4 = func1(4)
>>> func3(1)
4
>>> func4(1)
5
>>> func3.a=5
>>> func3(1)
4
>>> func1.a=5
>>> func3(1)
4
func3的def中的'a'实际上是什么,func3是如何得到它的?
更新1:
感谢您的回答。问题没有完全表达出来。
我想,当我们调用func3 = func1(3)时,程序中有两个对象,func1的代码,被调用的func1(3)的对象(符号表)。
我的问题是:
是否存在调用代码func1()产生的func2()对象,或者func3的值只是一个指向产生的func1(3)成员的指针,即func2()的指令?
是func1(3)中调用func3(1)的a,还是func2()的对象?
【问题讨论】:
标签: python django function class symbols