- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我的第一个问题,请保持温柔。
如问题所述,我在 LPTHW Ex。 39. 我需要帮助来理解 get
函数中的默认变量。我想我不太明白为什么它被定义为 default=None
作为参数(或者为什么 get_slot
函数做了同样的事情)并使用参数 default =default
当它剥离 v
时返回以供以后在 ex39_test.py
我可以毫不费力地找到其他任何事情的答案。这看起来很简单,但没有其他来源明确说明这个问题。
供引用: The LPTHW Ex. 39 can be read here.
我感到困惑的部分涉及 hashmap.py
中的这部分代码:
def get(aMap, key, default=None):
"""Gets the value in a bucket for the given key, or the default."""
i, k, v = get_slot(aMap, key, default=default)
return v
我了解该函数的作用,并且了解大部分功能。不过,这种默认设置让我很困惑。我还想尽可能多地了解原因,以便该概念在将来有用。
请 ELI5。
注意:我假设 ex39_test.py
最后一部分中的 'Does Not Exist'
(调用此函数的唯一部分)是 (re? )设置(?)默认值,但我不确定这是正确或完整的。
再次供引用:
city = hashmap.get(cities, 'TX', 'Does Not Exist')
print "The city for the state 'TX' is: %s" % city
最佳答案
您正在使用调用者传递给 get
函数的默认值(即 default
)来调用 get_slot
。如果调用者没有传递任何内容,则 default
为 None
,但如果他们确实传递了任何内容,则您希望传递任何内容,而不是总是传递 无
。
关于Python:LPTHW 示例。 39、获取函数,默认变量。 ELI5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28554265/
我在做example 47摘自“艰难地学习 Python”。 这是我的代码: from nose.tools import* from ex47.game import Room def test_r
我正在处理 LPTHW,但遇到了 Ex47 的属性错误。我浏览过这个网站并在谷歌上搜索帮助,但似乎找不到任何东西。 我得到的错误是这样的: Traceback (most recent call
这个问题在这里已经有了答案: Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of
Exercise 43 Learn Python the Hard Way 使用一个字典,其值是构造函数。字典是在类中定义的。我不明白当此类返回字典值之一时会发生什么。每次都会返回给定类的新对象吗?
我正在学习 Zed Shaw 的《艰难地学习 Python》,并且正在进行练习 43。 该练习是一个使用对象和类的游戏脚本。我已经复制了 Zeds 脚本,只是想让它运行。 我希望,如果我能看到它运行,
我目前正在通过 LPTHW 并且我要到 excercise 48这是我第一次碰壁。 这是给我的测试用例的第一部分 from nose.tools import * from ex48 import l
在浏览 LPTHW 时,我开始阅读此处的代码: https://github.com/BrechtDeMan/secretsanta/blob/master/pairing.py 我一直在努力理解为什
这是我的第一个问题,请保持温柔。 如问题所述,我在 LPTHW Ex。 39. 我需要帮助来理解 get 函数中的默认变量。我想我不太明白为什么它被定义为 default=None 作为参数(或者为什
我正在学习一个简短的 Python 教程,但我无法完成最后一个练习。这是app.py的源码 import web urls = ( '/', 'Index' ) app = web.appli
我正在做 Learn Python The Hard Way 的练习 48 并编写 lexicon 字典和 scan 模块来运行以下测试: from nose.tools import * from
我是通过 LPTHW 工作的新手 This is the link for it 。在 ex48 中,我得到了很多代码作为单元测试,我应该编写相应的 my_code 来进行测试(使用 Nose 测试,
我正在从“以困难的方式学习python 3”中的“项目骨架”中出现此错误。我仍然不知道这些到底是什么,只是按照以下步骤操作。那么为什么这个问题以及如何解决呢? C:\>pip install virt
我是一名优秀的程序员,十分优秀!