为什么我们不能直接使用this而不是self在以下示例中?
function SeatReservation(name, initialMeal) {
var self = this;
self.name = name;
self.meal = ko.observable(initialMeal);
}
Run Code Online (Sandbox Code Playgroud)
回答后,我了解到:
是的,没有必要在课堂上没有上下文切换.
但我会将这种方法用作"惯例",尽管没有必要.
我使用Python2.7.9在Mac OSX 10.10.5上安装了Nose easy_install.安装似乎是成功的:
Collecting nose
Downloading nose-1.3.7-py2-none-any.whl (154kB)
100% |????????????????????????????????| 155kB 2.3MB/s
Installing collected packages: nose
Successfully installed nose-1.3.7
Run Code Online (Sandbox Code Playgroud)
但是现在,当我在命令行上尝试使用nosetests的基本内容时,nosetests -h或者which nosetests我只是得到:
bash: nosetests: command not found
Run Code Online (Sandbox Code Playgroud)
我已尝试卸载,重新安装使用pip,尝试安装,sudo然后sudo nostests在其他帖子建议的测试脚本的目录中运行,但似乎没有任何工作.
安装的最初目的是使用nose来运行我为这些简单的web.py应用程序编写的测试脚本运行一些基本测试.但没有任何作用,只是不断获得command not found回应.
奇怪的是,当我在终端中打开Python解释器时,执行以下操作:
import nose
nose.main()
Run Code Online (Sandbox Code Playgroud)
我得到了预期的结果:
.
----------------------------------------------------------------------
Ran 1 test in 0.135s
OK
Run Code Online (Sandbox Code Playgroud)
很明显,它安装在某个地方.关于到底发生了什么的任何建议?
注意:我已阅读此帖和Alex Martelli的回复,但我并不完全/完全理解他的回答.这有点超出了我目前的理解.我想帮助更好地理解它.
我理解当您尝试以下for循环时:
for key, value in dict:
print key
print value
Run Code Online (Sandbox Code Playgroud)
你得到:
ValueError: too many values to unpack
Run Code Online (Sandbox Code Playgroud)
虽然您可以循环遍历字典,但只需使用以下内容获取密钥:
for key in dict:
print key
Run Code Online (Sandbox Code Playgroud)
任何人都可以提供一个稍微不那么高级的解释,为什么你不能使用键值,而不使用.iteritems()?来迭代字典?
我正在使用argparse输入并将其传递给一个函数,该函数将两个变量作为参数**kwargs.
这是我的功能:
import requests
import sys
import argparse
def location_by_coordinate(LAT, LNG, **kwargs):
if not kwargs:
coordinate_url = "https://api.instagram.com/v1/locations/search?lat=%s&lng=%s&access_token=%s" % (LAT, LNG, current_token)
r = requests.get(coordinate_url).text
else:
coordinate_url = "https://api.instagram.com/v1/locations/search?lat=%s&lng=%s&access_token=%s" % (LAT, LNG, current_token)
for key, value in kwargs.iteritems():
if 'DISTANCE' in kwargs:
distance = kwargs.get('DISTANCE')
if distance > 5000:
print distance
print "max distance is 5000m, value is reassigned to default of 1000m"
distance = 1000
coordinate_url = "https://api.instagram.com/v1/locations/search?lat=%s&lng=%s&access_token=%s" % (LAT, LNG, current_token)
r = requests.get(coordinate_url).text …Run Code Online (Sandbox Code Playgroud) 我正在阅读这篇Python 2.7教程,他们已经过去了raw_input(),它提到:
input()函数会尝试将您输入的内容转换为Python代码,但它存在安全问题,因此您应该避免使用它.
我尝试用谷歌搜索一些解释,但对我来说仍然有点不清楚; 什么是与input()vs的固有安全问题的简单解释raw_input()?
单元测试和Python的新功能,在单元测试的教程介绍中遇到了示例,其中使用with语句来捕获ValueError.
正在测试的脚本(invoice_calculator.py)是:
def divide_pay(amount, staff_hours):
"""
Divide an invoice evenly amongst staff depending on how many hours they
worked on a project
"""
total_hours = 0
for person in staff_hours:
total_hours += staff_hours[person]
if total_hours == 0:
raise ValueError("No hours entered")
per_hour = amount / total_hours
staff_pay = {}
for person in staff_hours:
pay = staff_hours[person] * per_hour
staff_pay[person] = pay
return staff_pay
Run Code Online (Sandbox Code Playgroud)
单元测试包括此功能,以便捕获边缘情况,其中staff_hours = None:
import unittest
from invoice_calculator import divide_pay
class InvoiceCalculatorTests(unittest.TestCase):
def test_equality(self):
pay = …Run Code Online (Sandbox Code Playgroud) python unit-testing with-statement python-2.7 python-unittest
Python新手在这里很抱歉我确定这是一个愚蠢的问题,但我似乎无法在一个教程中解决以下挑战,该教程要求我使用while循环来检查有效的用户输入.
(使用Python2.7)
这是我的代码,但它无法正常工作:
choice = raw_input('Enjoying the course? (y/n)')
student_surveyPromptOn = True
while student_surveyPromptOn:
if choice != raw_input('Enjoying the course? (y/n)'):
print("Sorry, I didn't catch that. Enter again: ")
else:
student_surveyPromptOn = False
Run Code Online (Sandbox Code Playgroud)
以上打印到控制台:
Enjoying the course? (y/n) y
Enjoying the course? (y/n) n
Sorry, I didn't catch that. Enter again:
Enjoying the course? (y/n) x
Sorry, I didn't catch that. Enter again:
Enjoying the course? (y/n)
Run Code Online (Sandbox Code Playgroud)
这显然是不正确的 - 当用户输入'y'或'n'时循环应该结束但我不知道如何做到这一点.我在这做错了什么?
注意:挑战要求我同时使用!=运算符和loop_condition
我正在尝试编写一个Python脚本,该脚本将搜索CSV文件并确定当两个项目彼此相邻出现时的出现次数。
例如,假设CSV如下所示:
red,green,blue,red,yellow,green,yellow,red,green,purple,blue,yellow,red,blue,blue,green,purple,red,blue,blue,red,green
Run Code Online (Sandbox Code Playgroud)
而且我想查找“红色,绿色”彼此相邻出现的次数(但我想要一个不仅仅针对此CSV单词的解决方案)。
到目前为止,我认为可能将CSV转换为列表可能是一个不错的开始:
import csv
with open('examplefile.csv', 'rb') as f:
reader = csv.reader(f)
your_list = list(reader)
print your_list
Run Code Online (Sandbox Code Playgroud)
哪个返回:
[['red', 'green', 'blue', 'red', 'yellow', 'green', 'yellow', 'red', 'green', 'purple', 'blue', 'yellow', 'red', 'blue', 'blue', 'green', 'purple', 'red', 'blue', 'blue', 'red', 'green ']]
Run Code Online (Sandbox Code Playgroud)
在此列表中,出现了三种情况'red', 'green'-什么是一种方法/模块/循环结构,我可以用来找出列表中两个项目在列表中彼此相邻的次数是否超过一次?
Python新手,很好奇是否有人可以尽可能用通俗易懂的方式解释为什么当你创建子类时,你仍然需要调用父类初始化函数?
假设您有一个这样的父类:
class Pet(object):
def __init__(self, name, species):
self.name = name
self.species = species
def getName(self):
return self.name
def getSpecies(self):
return self.species
def __str__(self):
return "%s is a %s" % (self.name, self.species)
Run Code Online (Sandbox Code Playgroud)
然后我想创建一个名为“Dog”的子类,它显然使用了父类中的一些函数。我的研究告诉我,我需要做这样的事情:
class Dog(Pet):
def __init__(self, name, chases_cats):
Pet.__init__(self, name, "Dog")
self.chases_cats = chases_cats
def chasesCats(self):
return self.chases_cats
Run Code Online (Sandbox Code Playgroud)
但是,如果我在定义子类时将父类作为参数传递,为什么我需要调用父类的呢__init__?这不是因为它是子类而自动传递给子类吗?
我确信我在理解中遗漏了一些明显的东西,但它只是不适合我。
我似乎无法找到解决方案......
我已经使用以下命令在运行 Python2.7 的 Mac OSX 10.10.5 上安装了 autopep8:
$ pip install autopep8
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试autopep8对这样的文件运行任何命令时:
$ autopep8 --in-place --aggressive --aggressive testfile.py
Run Code Online (Sandbox Code Playgroud)
我明白了:
-bash: autopep: command not found
Run Code Online (Sandbox Code Playgroud)
我尝试autopep8通过以下方式确认已安装:
>>> try:
... import autopep8
... print "this is installed"
... except ImportError:
... print "no module"
...
this is installed
Run Code Online (Sandbox Code Playgroud)
我也卸载并重新安装了autopep8几次pep8,但没有效果。我还应该尝试什么或者我做错了什么?
python ×9
python-2.7 ×6
command-line ×2
argparse ×1
autopep8 ×1
built-in ×1
class ×1
csv ×1
dictionary ×1
input ×1
javascript ×1
json ×1
knockout.js ×1
kwargs ×1
list ×1
macos ×1
nose ×1
security ×1
subclass ×1
unit-testing ×1
user-input ×1
while-loop ×1