小编use*_*464的帖子

Swift:switch语句中未解析的标识符

我正在苹果公司的快速之旅中在Xcode6-Beta4的操场上尝试以下内容:

let vegetable = "red pepper"
switch vegetable{
case "celery":
    let vegetableComment = "Add some raisins and make ants on a log."
case "cucumber", "watercress":
    let vegetableComment = "That would make a good tea sandwich."
case let x where x.hasSuffix("pepper"):
    let vegetableComment = "Is it a spicy \(x)"
default:
    let vegetableComment = "Everything tastes good in soup."
}
Run Code Online (Sandbox Code Playgroud)

然后我尝试调用vegetableCommentswitch语句中定义的变量,我得到了一个错误Use of unresolved identifier 'vegetableComment'

它是否与swift中switch语句的范围/闭包有关?

xcode closures scope switch-statement swift

1
推荐指数
1
解决办法
1705
查看次数

轻量级 Django:settings.ALLOWED_HOSTS 设置为环境变量

我正在关注 O'Reilly 的 Julia Elman 和 Mark Lavin 编写的轻量级 Django。在第一章中,我无法将 ALLOWED_HOSTS 设置为环境变量。

当我跑步时python hello.py runserver,我一直拥有CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False

这是我的hello.py

import os
import sys
from django.conf import settings

DEBUG = os.environ.get('DEBUG', 'on') == 'on'
SECRET_KEY = os.environ.get('SECRET_KEY', os.urandom(32))
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', 'localhost').split(',')

settings.configure(
    DEBUG=DEBUG,
    SECRET_KEY=SECRET_KEY,
    ROOT_URLCONF=__name__,
    MIDDLEWARE_CLASSES=(
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ),
)

from django.conf.urls import url
from django.core.wsgi import get_wsgi_application
from django.http import HttpResponse

def index(request):
    return HttpResponse('Hello World')

urlpatterns = …
Run Code Online (Sandbox Code Playgroud)

python django environment-variables

1
推荐指数
1
解决办法
8434
查看次数

Python:为特定函数创建临时名称

我正在学习Python.我正在阅读包含以下内容的代码:

class Menu:
    '''Display a menu and respond to choices when run.'''
    def __init__(self):
        self.notebook = Notebook()
        self.choices = {
            "1": self.show_notes,
            "2": self.search_notes,
            "3": self.add_note,
            "4": self.modify_note,
            "5": self.quit
            }

    def display_menu(self):
        print("""
Notebook Menu

1. Show all Notes
2. Search Notes
3. Add Note
4. Modify Note
5. Quit
""")

    def run(self):
        """Display the menu and respond to choices."""
        while True:
            self.display_menu()
            choice = input("Enter an option: ")
            action = self.choice.get(choice)
            if action:
                action()
            else:
                print("{0} is not …
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
44
查看次数

Python:切片deque

如何将a切片deque到某个部分,像list_numbers[:5]

我的梦想代码如下:

from collections import deque
deque_num = deque([1, 2, 3])
deque_sectioned = deque_num[:1]
Run Code Online (Sandbox Code Playgroud)

python deque

0
推荐指数
1
解决办法
49
查看次数