我有一段代码,我必须重复一遍,内部更改,所以我将内部分开更改为不同的函数,但这些函数可能需要不同的参数,所以我必须将这些可选参数传递给包含重复块的函数,以及包含更改部分的函数。
def scrapeWeb (url, function, *positional_parameters, **keyword_parameters):
retval = null
# Open connection
with contextlib.closing (urlopen(url)) as conn:
try:
rawHTML = conn.read ()
soupPage = soup (rawHTML, "html.parser")
retval = function (soupPage)
except:
print (traceback.format_exc())
print ("Couldn't download web")
return retval
def scrapePartOfWeb (*args):
retval = soupPage.find ("tr", {"class": "product"})
insertInDatabase (product, productID)
def scrapeProduct ():
url = "html..."
scrapeWeb (url, scrapeParOfWeb, optionalParameterProductID)
我的问题是在这个例子中我如何从函数scrapePartOfWeb
将参数传递给函数scrapeProducts
。
编辑:如何让条形码打印我传递给foo的参数?
def foo (x, function, *args):
function (x, *args)
def bar (x, product_name, product_id):
print (product_name, product_id)
def baz (x, string):
print (x, string)
def main ():
foo (x, bar, product_name, product_id)
foo (x, baz, string)
我知道每当我打电话给它时它会有两个参数而baz会有一个参数。
如果我想同时打电话给他们?
def main ():
foo (x, bar, baz, product_name, product_id, string)