[英]Django/Python API - how can I figure out what class of object a method is expecting to receive?
I just spent two hours debugging some django code because the API was not clear, in my opinion. 我认为调试django代码只花了两个小时,因为我认为API不清楚。 Here is what the Django documentation says about the "render" method: https://docs.djangoproject.com/en/1.3/topics/http/shortcuts/#render
这是Django文档中有关“渲染”方法的内容: https : //docs.djangoproject.com/en/1.3/topics/http/shortcuts/#render
render(request, template[, dictionary][, context_instance][, content_type][, status][, current_app])
渲染(请求,模板[,字典] [,上下文实例] [,内容类型] [,状态[[current_app]))
Combines a given template with a given context dictionary and returns an HttpResponse object with that rendered text.
将给定的模板与给定的上下文字典组合在一起,并返回带有该呈现文本的HttpResponse对象。
render() is the same as a call to render_to_response() with a context_instance argument that forces the use of a RequestContext.
render()与带有context_instance参数的render_to_response()调用相同,该参数强制使用RequestContext。
Now, seeing that the second argument that the render method expects is a template, I passed it a Template object. 现在,看到render方法期望的第二个参数是模板,我将其传递给Template对象。 It turns out that what the method really wants is the name of the template given as a String.
事实证明,该方法真正想要的是作为String给出的模板的名称。 How am I supposed to know this?
我应该怎么知道呢? And what is "status" -- an integer?
什么是“状态”-整数? a string?
一个字符串? a Status object?
状态对象? a list?
一个列表? Where can I get clear documentation to refer to?
在哪里可以获得清晰的文档以供参考?
Also, I would love to find an IDE that can detect these kinds of things while I'm programming -- when I worked with Netbeans and Java this sort of error would have been caught by the editor with a squiggly red underline before I'd even started typing the next line of code. 另外,我很想找到一个在编程时可以检测到这类事情的IDE -当我使用Netbeans和Java时,这种错误在被编辑之前会被编辑器捕获并带有红色的下划线甚至开始输入下一行代码。
I can't at all see how this could be any better explained. 我完全看不出有什么更好的解释。 As miku points out in the comment, the documentation clearly says "the full name of a template to use".
正如miku在评论中指出的那样,文档中明确指出“要使用的模板的全名”。 Plus, the example given directly underneath shows the use of a string.
另外,下面直接给出的示例说明了字符串的使用。
Python is not Java, nor any statically typed language. Python不是Java,也不是任何静态类型的语言。 The documentation doesn't say it wants a string, because it doesn't: it wants something string-like that contains the name.
文档没有说它想要一个字符串,因为它不是:它想要一个包含名称的类似字符串的东西。
And don't forget, you've got immediate access to the code. 并且不要忘记,您可以立即访问该代码。 Don't understand the function?
不了解功能? Well, look at
https://code.djangoproject.com/browser/django/trunk/django/shortcuts/__init__.py
and there it is. 好吧,看看
https://code.djangoproject.com/browser/django/trunk/django/shortcuts/__init__.py
。
Even better, in Python you have an option that you simply don't have in Java: look in the shell. 更好的是,在Python中,您有一个Java根本没有的选项:在shell中查看。 Do
from django.shortcuts import render
, then do help(render)
to see what it says, and if that doesn't help try calling it with a few different arguments right there to see what it does. from django.shortcuts import render
,然后执行help(render)
来查看其内容,如果这样做没有帮助,请尝试使用几个不同的参数调用它以查看其作用。
If you insist on programming Python like it is Java, you will find it difficult. 如果您坚持像对Java一样对Python进行编程,就会发现这很困难。 If you program Python like it's Python, you will find it much simpler.
如果像使用Python一样编程Python,您会发现它更加简单。
While I agree with Daniel Roseman's answer, I do sympathise with your bewilderment, and I do think the interface and its parameters could have been better explained. 虽然我同意Daniel Roseman的回答,但我确实对您的困惑感到同情,并且我认为可以更好地解释该接口及其参数。
Django's interface is very committed to the idea of dynamic typing, or " duck typing " — as in, if it walks like a duck, if it swims like a duck, then call it a duck. Django的界面非常致力于动态类型化或“ 鸭子类型化 ”的想法-例如,如果它走路像鸭子一样,如果它像鸭子一样游泳,则称它为鸭子。 Django's interface doesn't require you to pass a string type, it requires you to pass an entity which behaves like a string.
Django的界面不需要您传递字符串类型,而是需要您传递行为类似于字符串的实体。
Look carefully, in the documentation to which you linked, at the specific wording for each parameter to render()
. 在您链接到的文档中,仔细查看
render()
每个参数的特定措辞。 request is "the request object ". request是“请求对象 ”。 template is "the full name ... or sequence of names ."
模板是“全名 ...或名称 序列 ”。 dictionary is "a dictionary of values...".
字典是“价值字典 ...”。 status is "the status code for the response. Defaults to 200 " [an integer].
status是“响应的状态代码。默认为200 ” [整数]。
I've added emphasis to words which hint at which dynamic type the interface expects or returns. 我在单词中增加了重点,以提示界面期望或返回哪种动态类型。 object means an object type, with a set of methods.
对象是指具有一组方法的对象类型。 name implies string.
名称表示字符串。 The example of 200 implies that status code is an integer.
200的示例表示状态码是整数。
I think the Django documentation would be clearer if it were more explicit about its vocabulary of the dynamic types it expects or returns. 我认为,如果Django文档更加明确地说明了它期望或返回的动态类型的词汇,它将更加清晰。 It could say something like, " request is an object implementing the Request Object Interface", and then define exactly what methods an object needs to implement to qualify as that interface.
它可以说“ 请求是实现请求对象接口的对象”之类的内容,然后准确定义对象需要实现哪些方法才能成为该接口。 The documentation could also be explicit instead of implicit in cases like status , where it could say "integer" literally.
在status之类的情况下 ,文档也可以是显式的,而不是隐式的,在文档中它可以字面意义上说“整数”。
But those caveats aside, Django's interface is designed to be "pythonic", to embrace duck typing. 但是除了这些警告之外,Django的界面被设计为“ pythonic”的,以包含鸭子类型。 Daniel is right, "if you insist on programming Python like it is Java, you will find it difficult."
Daniel是对的,“如果您坚持像对Java那样对Python进行编程,就会发现这很困难。” This might be a good time to read over Django's Design Philosophies and Python's PEP 20 -- The Zen of Python .
这可能是阅读Django的设计哲学和Python的PEP 20-Python之禅的好时机。 Note that neither mentions "compile-time syntax checking thanks to strong typing" as a goal.
请注意,它们都没有提到“由于强类型化而进行编译时语法检查”作为目标。 Toto, I don't think we're in Java anymore.
Toto,我认为我们不再使用Java。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.