首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当参数“看起来像赋值”- foo(a=b)时,实际传递给foo()的值是多少?

当参数“看起来像赋值”- foo(a=b)时,实际传递给foo()的值是多少?
EN

Stack Overflow用户
提问于 2016-12-09 09:35:12
回答 1查看 53关注 0票数 1

编辑:我不认为我的问题是重复的(见下面的评论),因为这个问题假设你已经知道“位置参数”的存在。如果我知道位置论点--更好--甚至在下面的最初评论中犯了一个错误--如果我知道了keyword arguments,我就不需要问这个问题了!我所能想到的就是“看上去像一项任务”,就像我在C中看到/知道的。

附注:我读过其他答案--如果没有下面的答案,我现在就不会用答案来回答我的问题“如何阅读”这段代码。下面的答案给了我一个“终身”的理解,使我能够阅读foo(a=b),然后继续前进。此外,我还学习了“关键字论证”这个术语--但我也很高兴,不知道:)

我也觉得它更简洁-直接回答我的需要。我将不得不花更多的时间来学习我从这个答案中学到的东西--从“其他”答案中学到的东西。

问题的关键是:考虑到python调用: util.find_devs_with(path=device),实际发生了什么(下面的代码细节/摘录)?

  1. 创建变量"path",将“设备”的值分配给它。
  2. 传递刚刚分配给util.find_devs()的值
  3. 那么这两个电话中的哪一个是:(还是其他什么?)

或者它是否做了以下工作:

代码语言:javascript
复制
# blkid -t/dev/sr0 2&>/dev/null
# echo $?
4

# blkid -tpath=/dev/sr0 2&>/dev/null
# echo $?
2

详细信息

代码语言:javascript
复制
POSSIBLE_MOUNTS = ('sr', 'cd')
OPTICAL_DEVICES = tuple(('/dev/%s%s' % (z, i) for z in POSSIBLE_MOUNTS
                        for i in range(0, 2)))
...
def find_candidate_devs(probe_optical=True):
    """Return a list of devices that may contain the config drive.

    The returned list is sorted by search order where the first item has
    should be searched first (highest priority)

    config drive v1:
       Per documentation, this is "associated as the last available disk on the
       instance", and should be VFAT.
       Currently, we do not restrict search list to "last available disk"

    config drive v2:
       Disk should be:
        * either vfat or iso9660 formated
        * labeled with 'config-2'
    """
    if probe_optical:
        for device in OPTICAL_DEVICES:
            try:
                util.find_devs_with(path=device)
            except util.ProcessExecutionError:
                pass
...

在util.py中

代码语言:javascript
复制
def find_devs_with(criteria=None, oformat='device',
                   tag=None, no_cache=False, path=None):
    """
    find devices matching given criteria (via blkid)
    criteria can be *one* of:
      TYPE=<filesystem>
      LABEL=<label>
      UUID=<uuid>
    """
    blk_id_cmd = ['blkid']
    options = []
    if criteria:
        # Search for block devices with tokens named NAME that
        # have the value 'value' and display any devices which are found.
        # Common values for NAME include  TYPE, LABEL, and UUID.
        # If there are no devices specified on the command line,
        # all block devices will be searched; otherwise,
        # only search the devices specified by the user.
        options.append("-t%s" % (criteria))
...
    cmd = blk_id_cmd + options
    # See man blkid for why 2 is added
    try:
        (out, _err) = subp(cmd, rcs=[0, 2])
    except ProcessExecutionError as e:
        if e.errno == errno.ENOENT:
            # blkid not found...
            out = ""
        else:
            raise
    entries = []
    for line in out.splitlines():
        line = line.strip()
        if line:
            entries.append(line)
    return entries

我的期望是正在执行的命令相当于:

代码语言:javascript
复制
blkid "-t%s" % criteria

那么,“接收”作为标准的价值是什么呢?也就是巨蟒论证“堆叠?”的一课。

第二次检查所有这些代码时,我猜参数'path=device‘只是"somegarbage“,因此会调用blkid -tsomegarbage,并在填充blkid缓存后返回一个错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-09 14:03:09

在Python中,可以用默认值分配参数。鉴于这一签名:

代码语言:javascript
复制
def find_devs_with(criteria=None, oformat='device',
                   tag=None, no_cache=False, path=None):

如果您调用它时:

代码语言:javascript
复制
util.find_devs_with(path=device)

则存在下列作业:

代码语言:javascript
复制
criteria = None
oformat = 'device' 
tag = None
no_cache = False
path = device   # according to the above code, /dev/sr0, ..., /dev/cd1

因为criteriaNone,所以没有附加-t选项。执行的命令是简单的blkid

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41062897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档