我正在尝试django.我在urls.py中使用了namespace我include()的一个参数.当我运行服务器并尝试浏览时,我收到此错误.
File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\conf.py", line 39, in include
'Specifying a namespace in include() without providing an app_name '
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.
Run Code Online (Sandbox Code Playgroud)
这些是我的urls.py文件:
#project/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^reviews/', include('reviews.urls', namespace='reviews')),
url(r'^admin/', include(admin.site.urls)),
]
Run Code Online (Sandbox Code Playgroud)
和
#app/urls.py
from django.conf.urls import url …Run Code Online (Sandbox Code Playgroud) 在 python 中,可以定义一个采用任意数量的位置参数的函数,如下所示:
def f(*args):
print(args)
f(1, 2, 3) # (1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
当调用 as 时f(a, b, c),所有位置参数都被放入一个元组中。python 2和3文档中描述了此行为,但我还没有找到它的 PEP。
PEP 3132,在“接受”下引入扩展的可迭代拆包 ( first, *middle, last = seqence) 状态
使加星标的目标成为元组而不是列表。这将与函数的 *args 一致,但会使结果的进一步处理变得更加困难。
进行了讨论。如果我编写一个包装器,我可能还想进一步处理参数,如下所示:
def force_type(position, type):
def wrapper(f):
def new(*args, **kwargs):
args = list(args) # Why?
args[position] = type(args[position])
return f(*args, **kwargs)
return new
return wrapper
@force_type(1, int)
def func(a, b, c):
assert isinstance(b, int)
Run Code Online (Sandbox Code Playgroud)
args事实上,进一步的处理变得更加困难tuple。难道在引入的早期阶段就没有使用包装纸吗?如果是这样,为什么在 python3 …
我正在尝试创建一个程序来轻松处理 IT 请求,并且我已经创建了一个程序来测试我的网络上的 PC 是否在列表中处于活动状态。
为此,我编写了以下代码:
self.btn_Ping.clicked.connect(self.ping)
def ping(self):
hostname = self.listWidget.currentItem().text()
if hostname:
os.system("ping " + hostname + " -t")
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我的主程序冻结,并且在关闭 ping 命令窗口之前我无法执行任何操作。对此我能做什么?我可以使用任何其他命令来尝试 ping 机器而不会使我的主程序冻结吗?
我偶然发现了yield今天的一个有趣的行为,我并不理解.这是我的代码:
def a():
def b(x):
print("entering b.")
yield 0
if x == 0:
print("calling b.")
b(x + 1)
print("return from b.")
print("leaving b.")
for x in b(0):
yield x
for x in a():
print(x)
Run Code Online (Sandbox Code Playgroud)
那输出:
entering b.
0
calling b.
return from b.
leaving b.
Run Code Online (Sandbox Code Playgroud)
令我困惑的是显式调用b(x + 1)不调用b(!),Python也没有给出任何错误或异常.
现在,显然上面代码中的错误是b(x + 1)应该真正产生产生的值b- 所以它应该读取如下内容:
for x in b(x + 1):
yield x
Run Code Online (Sandbox Code Playgroud)
事情就好了.
不过,这是yield我应该注意的吗?