编辑:我不认为我的问题是重复的(见下面的评论),因为这个问题假设你已经知道"位置参数"存在。如果我知道位置论证 - 更好 - 甚至在我下面的初步评论中犯了一个错误 - 我知道keyword arguments
我不需要问这个问题!我能想到的只是"看起来像一个任务"正如我在C中看到/知道的那样。
P.S。我已经阅读了其他答案 - 如果没有下面的答案,我现在不会如何使用答案回答我的问题"如何阅读"这段代码。 下面的答案给了我一个终生的"理解这样我可以阅读foo(a = b),继续前进。另外,我已经学会了术语"关键字参数" - 但我也很高兴不知道:)
我也觉得它更简洁 - 直接回答我的需要。我不得不花更多的时间来学习从这个答案中学到的东西 - 来自其他的'答案。
问题的关键是: 考虑到python调用: util.find_devs_with(路径=装置) 实际发生了什么(代码详情/摘录如下)?
或者它是否正在执行以下操作:
# blkid -t/dev/sr0 2&>/dev/null
# echo $?
4
# blkid -tpath=/dev/sr0 2&>/dev/null
# echo $?
2
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
...
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
我的期望是正在执行的命令等同于:
blkid "-t%s" % criteria
那么,收到的价值是什么?#34;作为标准?又是python参数的教训&#34;堆叠?&#34;。
第二次浏览所有这些代码,我的猜测是“#path; path = device&#39;只是&#34; somegarbage&#34;这样就可以调用blkid -tsomegarbage,并在填充blkid缓存后返回错误。
答案 0 :(得分:2)
在Python中,可以使用默认值分配参数。鉴于此签名:
def find_devs_with(criteria=None, oformat='device',
tag=None, no_cache=False, path=None):
如果你用:
调用它util.find_devs_with(path=device)
然后存在以下分配:
criteria = None
oformat = 'device'
tag = None
no_cache = False
path = device # according to the above code, /dev/sr0, ..., /dev/cd1
由于criteria
为None
,因此不会附加-t
选项。执行的命令只是blkid
。